Should I comment printlin() in the sake of the performance?

Hello there! Hope this finds you well.

I'm working in a small project, which consists in an IoT sensor network, which uploads the readings to a Google Spreadsheets file, my doubt, and the purpose of this post is, as you know, any code has println() commands to check it's behavior during the tests and prototypes... So I'm planning to just comment these afore said commands to save resources during the actual executions of my code. My question is: commenting the println() will actually optimize my code performance? Or it won't make such difference?

I wouldn't like to delete those commands in order to easily understand my code structure in the future. And don't loose too much time improving it in the future if the chance is set.

Thanks in advance!

Removing unneeded serial prints helps realize a performance increase.

you could use macros for the debug comments. Once they are not needed, just change the debug flag and recompile, all the calls are gone (and you get some memory back)

#define DEBUG 1    // SET TO 0 OUT TO REMOVE TRACES

#if DEBUG
#define D_SerialBegin(...) Serial.begin(__VA_ARGS__);
#define D_print(...)    Serial.print(__VA_ARGS__)
#define D_write(...)    Serial.print(__VA_ARGS__)
#define D_println(...)  Serial.println(__VA_ARGS__)
#else
#define D_SerialBegin(bauds)
#define D_print(...)
#define D_write(...)
#define D_println(...)
#endif
2 Likes

The risk with Serial printing is that it will block when the output buffer is full. The lower the baud rate and the more stuff you print, the more likely that becomes.

If you're not blocking, I wouldn't expect much of a performance difference, especially if you're waiting for responses from a cloud server. Note though, that you should test thoroughly once you comment the prints out because in some cases, the time taken to print was masking some other timing issue.

Personally, I leave the prints alone until I can prove to myself that they're causing a problem.

1 Like

My project gotta be ON during long periods, so...

Will my solution of commenting all the println() commands, should work just fine, right?

One of my circuits does use the serial port. I always set my baud rate at 115200 tho.

Probably, but it's possible that your change will cause a Heisenbug as I mentioned earlier.

1 Like

if the code is fine, with no bugs, memory leak, no timing dependencies, etc then sure you'll be fine by commenting out the Serial stuff.

As mentioned above, because this is tedious, using the macros can help comment or uncomment by just changing 1 character...

1 Like

Thank you so much! I'll try that!

I have used

boolean verbose = true; // (or false)


if(verbose) Serial. xxx

You can change the control variable to focus on certain sections of your sketch

I have also used something like

byte debugLevel = 2;


if (debugLevel > 1) Serial.print…
if (debugLevel > 2) Serial.print…

and so forth. This lets me control how much debugging output I get or where to focus the debugging

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.