Serial.write(0x0d); // hex

Serial.write(13); // ASCII

Serial.write('\r'); // char

Is one more efficient? I read how it depends on the terminal program. So does that mean the IDE? (This is my first time working with serial comm.)

  • It’s \r not /r.
    – Gerben
    Jul 23 '18 at 15:07
  • 2
    All of them are identical. But the last one is the most readable to us humans. So I’d prefer the last one.
    – Gerben
    Jul 23 '18 at 15:08
  • 1
    it depends on the terminal program ... no it does not. .... the terminal program will receive ASCII CR for each of the lines
    – jsotola
    Jul 23 '18 at 15:40

As mentioned before they are all the same. The thing you have to rememeber is that the compiler takes the human readable code you write and turns it into the instructions the processor can understand. If you want to explore ways the compiler does thing you can compile the program into the hex file and then compare. Or for even less detail, but much easier, look at the size of the file created when you compile in the IDE using just the one line in question inside the setup function

write(0x0d)  ----> 1334 bytes
write(13)    ----> 1334 bytes
write('\r')  ----> 1334 bytes

For this simple example we can assume that all three of these are producing the exact same hex file. There are many more complicated example of different ways to do the same task that may produce more or less efficient code, but in this case the compiler is going to produce the same instruction set to the processor.

As pointed out in the comments, I am not saying that because they are the same size they must be the same hex code. As any single byte being written would produce the same size code, but not the same result. But when comparing two methods that we know are the same functionality ( 0x0d and '\r') and they produce the same size binary, then we can assume the compiler reduced them to the same code.

  • 1
    write('\t') would result in same size
    – Juraj
    Jul 23 '18 at 15:34
  • yes, as would any other single byte. and without having the ability to analyze the hex file, or other ways of getting a gaining a detailed idea of how efficient the code is, you could deduce that they were equally efficient. Although there are many cases this idea would not hold. It does give a new programmer an Idea of whats going on under the hood.
    – Chad G
    Jul 23 '18 at 15:46

There is no difference between all the lines of code in your question. All representations stand for one value, containing one byte. There is only one implementation of Serial::write(), which has a uint8_t as parameter. So all the different representations result in a uint8_t anyway.


Is one more efficient?

after compilation it is same for all notations. the function and the value are the same for all 3 notations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.