Native USB port speed not as expected?

Hi everyone!

I'm trying to use Arduino Due for my little project and was expecting the communication bandwidth to be at least ~15MB/s for my use. I saw that the Native port supports up to 480MB/s throughput while Arduino due runs at ~80MHz, so I assumed it would definitely satisfy my request.

However, I tried to implement a very simple program testing if I could get the communication right. The result shows the speed is only around 520KB/s. I'm posting my code below - Generally due reads some unsigned int data from the SerialUSB then returns them back. I tried to send 2560 packages and it took about 20 seconds too complete. With the array length of each package being 256 the speed is roughly only 520KB/s, far away from the 480MB/s or 80MHz clock speed can do.

I'm very new to Arduino and definitely do not understand how it works very well. Is this just the normal speed or did I do something wrong? Could it be Serial buffer issue or the power supply to the board (I'm only using USB as a power source)? Thank you!

const int length_pack=256;
unsigned int data_in[length_pack];
unsigned int n[2];


void setup() {
  SerialUSB.begin (230400); //does not matter for Native Port
  while(!SerialUSB){}
}

void loop() {
	if (SerialUSB.available()) {
		SerialUSB.readBytes((uint8_t*)n,4)
		SerialUSB.readBytes((uint8_t*)data_in,length_pack*4);
		SerialUSB.write((uint8_t*)data_in,length_pack*4);
	}

}

While the Due theoretically supports high-speed USB, I doubt whether the Due Serial driver, or the CDC Driver in most host implementations, will implement that sort of speed on a COM port.

For speed testing, I suggest doing writes in big chunks. Reading/writing
4 bytes at a time is very inefficient for USB. When using 480 Mbits/sec
USB the fundamental packet size is 512 bytes. Try writes in 512, 1024,
2048, or 4096 chunks.

Whatever you use on the PC should also read in multiples of 512 bytes.
Also for max speed, test reading and writing separately.

Thanks for your suggestion Icebuster! However, I think arduino only provides readBytes function for receiving binary data, and as I've tested, weirdly if the board sends out more than 256 unsigned int (1024 bytes, using SerialUSB.write) at a time the read on the PC side would fail. Do you have any suggestion for a better way of implementing this?

That's indeed what I'm worried about...

The Teensy 3.2 would probably be a better choice. Paul Stoffregen spent a lot of time optimizing the native USB serial data transfer.