substring

Must have been asked a million times, but...

What would be the fastest way to print the first 10 characters of an array of chars to Serial?

Thanks,
Kevin

Unroll a loop:

Serial.print(mystring[0];
Serial.print(mystring[1];
Serial.print(mystring[2];
//...

ETA:
It might be faster to cache the 11th character and replace it with a temporary '\0' and print the string., then replace the 11th character. It depends on the Serial.Print() overhead.

Kevin77:
What would be the fastest way to print the first 10 characters of an array of chars to Serial?

Use the fastest baudrate that is supported by sender and receiver as well!

When using "Serial" on an Arduino as sender and a PC as the receiving station, the highest possible baudrate should be 500000 baud. As 500000 baud are not supported by the "Serial Monitor" included in the Arduino Software, you'd have to use a third-party "serial terminal software" on the PC.

Thanks Jurs,

Could another Arduino receive at this speed?

Also would a for loop be fastest for this? (Loop over the first 10 characters of a string)

Thanks for your fast reply!

No, my unrolling of the loop would be faster than a for, no increment and test.
(Unless gcc went ahead and unrolled it for you!)

Thanks Keith,

more questions i am afraid :slight_smile:
Is there a way to find out if gcc does that or not?
If so do you think it would matter at Serial speeds the Arduino can uderstand?

You need to look at the assembler output.
As I said above it all depends on the overhead of the .Print() call.

Serial takes so long anyway, it is not worth worrying about.

The fastest way? Most likely stuffing a zero into the 11th character (to make it a 10 character string), send the array to Serial.print, then restore the original value to the 11th character.

That said, I can’t imagine why speed on that operation should EVER be that important in a well-written application…

Regards,
Ray L.

Kevin77:
Could another Arduino receive at this speed?

Yes, should be possible for all 16 MHz boards.
But should be very short connections (a few cm only) between the boards when sending at that high baudrate.

Kevin77:
Also would a for loop be fastest for this? (Loop over the first 10 characters of a string)

It really doesn't matter how quickly you stuff the chars into the Serial send buffer and if the stuffing of bytes is done in 1µs or in 10µs. This makes no significant difference in the speed the characters are sent.

The latency time of the USB-Serial converter is several times higher than 9µs.

The slow thing is actually sending the bytes over Serial. Even with a baudrate of 500000 baud you are able to send "only" 50000 characters per second, meaning 1 character per 1/50000s = 20µs.

Does it really matter within your application, if there would be a small amount of microseconds latency time extra in sending the first character?

Hi, Jurs,

jurs:
Does it really matter within your application, if there would be a small amount of microseconds latency time extra in sending the first character?

I think not. Certainly not now I know i can sent at 500000 baud.

You guys helped me out a lot. I did some testing it worked... I never realized the Arduino's could talk to each other at that speed... I also didn't realize the bottle neck would be the Serial.print.

I am using a small piece of shielded cat5 cable. Any idea on where I might find some information on how long cables can be at what speed?