Serial monitor baud rates

Hi,

not a problem but I’m just wondering why I’m not seeing a big difference between baud rates in the serial monitor using the following code

void setup(){
Serial.begin(9600);
long startTime=micros();
Serial.println("1234"); 
//Serial.println(1234);
long finishtime=micros()-startTime;
Serial.println(finishtime);
}

doesn’t make much difference what speed I set the baud rate the finishtime variable stays pretty much the same, also there’s a big difference in sending “1234” and 1234, sending a char string is much faster than sending a number, is this due to a conversion overhead?

Nope, the code you use measures the time between the start off storing data in the buffer and the time this has been completed. The actual sending of data from the UART happens in the background and will take longer depending on the BAUD-rate. If you want to measure the time i guess you could locate the output buffer size variable (or both pointers head & tail) and wait until they are equal again.

Mostly a repeat of what Deva_Rishi replied while I was writing it, but there's some additional information so I'll post it anyway:

Leveller:
I'm just wondering why I'm not seeing a big difference between baud rates in the serial monitor using the following code

Arduino's hardware serial system does asynchronous communication. This means the Serial.println() just puts the string in the transmit buffer. After that, the serial data is sent out in the background via an interrupt service routine. The only exception to this asynchronous system is if the serial buffer is full. In that case, Serial.println() will block until there is enough space in the buffer for the entire string.

If you want to test how long it takes to clear the buffer at different baud rates, you can call Serial.flush(). This function blocks until the transmit buffer is empty:
https://www.arduino.cc/reference/en/language/functions/communication/serial/flush/

Leveller:
there's a big difference in sending "1234" and 1234, sending a char string is much faster than sending a number, is this due to a conversion overhead?

Correct. The Print class converts any standard type passed to the print() or println() functions to a string before putting the data in the buffer. So starting with a string will always be the fastest, whereas converting from another type to a string will take longer.

Try:

void setup(){
  Serial.begin(9600);
  unsigned long startTime=micros();
  Serial.println("1234");
  Serial.flush(); //block untill finished
  unsigned long finishtime = micros() - startTime;
  Serial.println(finishtime);
}

void loop(){
  
}

PS Use unsigned long when dealing with time! (millis() an micros()) Because you know you're right at the start here it's not a problem. But can cause you weird trouble in the future. :wink:

Perfectly answered, many thanks guys.

@ septillion, ah yes, overflow into negative value and unsigned can hold twice the positive value.