The result is 84 microseconds, regardless of the speed. I tried 9600, 57600 and 115200 bps and I always have the same speed.
How to explain that ?
IMHO, the above code would give you the program excecution time NOT the communication speed... for the latter you would need to use a scope if you want to confirm the data transfer rate.
Thanks.
I did this test because I wanted to improve the execution speed of a program with many serial.print.
But it seems that the duration of this function is independant of communication speed...
I did another test where I sent various data types (string, integer, double), and execution time slightly increase with com speed !
I have done measurements of transmission speed and it seems to be as expected - for example at 57600 baud it will send about 5760 bytes per second and at 500,000 baud it will send about 50,000 bytes per second.
Try measuring the time to send (say) 100 messages of 64 chars each. Just use Serial.flush() on the last message.
Thanks Robin,
Your answer makes me understand, at last, what Serial.flush() does.
From Arduino reference, "Serial.flush() waits for the transmission of outgoing serial data to complete", which was not clear to me.
In fact, Serial.flush() suspend program execution until the transmission of outgoing serial data is completed.
I wasn't aware that serial transmission occurs in background, so after a call to Serial.print, the program keeps running even if the transmission is not completed.
So here are the results with your code :
Without Serial.flush : 84 µs for any com speed ; after that time, data sent is still in the pipe !