Testing Arduino code efficiency

Ya’akov

Joined Jan 27, 2019
10,238
The only exception to that would be code that needs to be particularly fast or lightweight. Then those things are part of the requirements. But most code doesn't need to worry about that since we have so much CPU and memory, even MCUs today.

What I said about premature optimization is apt here. First write clear, maintainable, well commented code. Then test it. Then if it is too slow or too big, refactor it after profiling to see where the problems lie. You don't ignore making it small enough and fast enough while writing it, but you don't worry about it either.
 

MrChips

Joined Oct 2, 2009
34,820
If your code is properly structured it is likely to be efficient already. Leave it up to the optimizing compiler to use registers and remove redundancies.

The extreme case is when you have to hand code your program into assembler.
Bill Atkinson wrote QuickDraw for the first Macintosh computer in Pascal. Then he hand-coded it into ASM before burning it into ROM.
 

nsaspook

Joined Aug 27, 2009
16,330
So when making a library, most would encourage simple, good, legible code over strictly optimized?
I would encourage simple, good, legible code over strictly optimized for all software. This is not saying the compiler is magic and will convert poorly structured C code into good machine code.

Write (and learn about writing) your C code in a structured manner so it's easy to understand and debug in code and data-flow. Unless there is a 'true' problem with code speed or size don't worry much about C source code optimization. Excessive source code optimization usually obscures the HLL human language structure and rarely results in better object code from a good compiler (that will turn your nice source code into an assembly language mess of tables and gotos as the optimized solution).

I think we sometimes forget there is a difference between 'Complexity' and complicated. IMO the assembler mindset is one that enjoys moving many simple parts in a complex way that come together to solve a complicated problem. The level of program complication remains the same with the C programmer of the same problem but the complexity of the parts to get to the solution is reduced in most cases. The complicated problem might just be extrinsically hard so nothing can be done to reduce it but usually there are ways to simplify it into bite-sized chunks, each with a less complex solution that can be optimized in any language.

Simple is better than complex
Complex is better than complicated.
from the The Zen of Python
 

Thread Starter

LikeTheSandwich

Joined Feb 22, 2021
206
My advice: if you want optimized code then stop using Arduino.
Maybe someday. But for now, it's really easy and simple and upon used to it. I kind of want to experiment with Micro Python or Circuit Python, but I have basically no experience with Python. I've been programming in C and Java off and on for 15 years so the syntax in Arduino IDE is familiar, and most of the code needs little to no rework to work between very different chips. I also don't like how in those two versions of Python it's really easy to accidentally delete your code. In Arduino saves are automatic. I'm running Linux, is there something that's considerably better than Arduino IDE for programming on RP2040, SAMD21, ESP8266/ESP32, AVR, and whatever other MCUs I might acquire that would likely be worth learning? Keep in mind that I'm strictly a hobbyist, not a professional when it comes to MCU stuff.
 

MrChips

Joined Oct 2, 2009
34,820
For sure, Arduino has its benefits. It was intended for hobbyists who have little or no knowledge of how MCUs work. It is widely popular and supported, with lots of open source libraries written by third party individuals for a wide range of peripherals and applications. It is easy to get from A to B very rapidly.

Personally, when I deploy an MCU I like to have control and know what code is going into it. If I am going to use an Atmel AVR MCU, I am going use Atmel Studio or IAR Embedded Workbench.
 

Thread Starter

LikeTheSandwich

Joined Feb 22, 2021
206
For sure, Arduino has its benefits. It was intended for hobbyists who have little or no knowledge of how MCUs work. It is widely popular and supported, with lots of open source libraries written by third party individuals for a wide range of peripherals and applications. It is easy to get from A to B very rapidly.

Personally, when I deploy an MCU I like to have control and know what code is going into it. If I am going to use an Atmel AVR MCU, I am going use Atmel Studio or IAR Embedded Workbench.
So as strictly a hobbyist, is it worth the time to learn other environments, like AVR's dedicated one, when I use a half dozen or so different brands of MCUs?
 

MrChips

Joined Oct 2, 2009
34,820
I too use a half-dozen or more different MCUs and brands on different platforms. That's no big deal to me. Basically I edit code, compile, execute. Same difference on all platforms.
 

MrSalts

Joined Apr 2, 2020
2,767
Using what, millis()?
You can just send millis() to the serial monitor at the beginning and end of your code (or after 100 cycles or what ever consistent thing you want. Then make your "efficiency change" and try again. Compare. - if you are optimizing on processing speed.
If you are trying to optimize on something else, then you need to measure that. The few clock ticks to send a value over to the serial Monitor is not a big deal because it is consistent - it will take the same amount of time no matter what "optimized version" of your code you are running so it is a simple offset.
There was discussion long ago that the Arduino IDE has some fat in "while statements" that makes it a quite a bit slower than "for loops" (I never looked into it but I remember reading thst somewhere). Have fun playing with various optimizations. Examples... Avoid floating point math and avoid division (which creates floats). Use bit-shifts if possible instead of divide or multiply by powers of 2, use byte and char variables instead of int for small values.
 

MrSalts

Joined Apr 2, 2020
2,767
So as strictly a hobbyist, is it worth the time to learn other environments, like AVR's dedicated one, when I use a half dozen or so different brands of MCUs?
If learning other environments is part of your hobby, then, yes. Otherwise, no. After all, it is your hobby. You decide if you want it. I know PIC (c and asm) and Espressif's environment plus Arduino - plus many other languages not related to micros. In the end, they are all the same at some level and each has some special parts that can be helpful in certain situations.
 

nsaspook

Joined Aug 27, 2009
16,330
Examples... Avoid floating point math and avoid division (which creates floats). Use bit-shifts if possible instead of divide or multiply by powers of 2, use byte and char variables instead of int for small values.
All of those tricks are things you do when you need heavy math on math light hardware. If you need math use something (hardware math processing on 16 or 32-bit controllers) that handles full IEEE floats faster that a 8-bit controller can bit-shift so you can write structured math code instead of 1990's style bit tricks. It's 2022 guys.
 

Thread Starter

LikeTheSandwich

Joined Feb 22, 2021
206
Oh, also, you can use VSCode with PlatformIO which is a much better environment than the Arduino IDE.
Well to each their own. I just tried it out, and I really don't like how it's set up. For a professional environment it might be a lot better than Arduino IDE, but I really like how minimalist Arduino IDE is in comparison. Creating a whole new project and having to select the board for the project feels like way too much for me, especially since I regularly put the same project on different boards and evolve the projects as I go. Being able to just change the board target in settings in Arduino IDE is preferable to me. But thank you for the suggestion, I'm glad I gave it a try.
 

Ya’akov

Joined Jan 27, 2019
10,238
Well to each their own. I just tried it out, and I really don't like how it's set up. For a professional environment it might be a lot better than Arduino IDE, but I really like how minimalist Arduino IDE is in comparison. Creating a whole new project and having to select the board for the project feels like way too much for me, especially since I regularly put the same project on different boards and evolve the projects as I go. Being able to just change the board target in settings in Arduino IDE is preferable to me. But thank you for the suggestion, I'm glad I gave it a try.
It is definitely more complex, but the VSCode editor is much better than the Arduino IDE. I still use the Arduino IDE though. Mostly because the learning curve for VSCode hasn't been worth it to me yet. But because I want to use Electron, and have some Javascript stuff I will be doing for the web, I want to learn it anyway. Then I will see if I prefer it to the Arduino IDE.

Oh, and the other reason I use Arduino IDE is for Teensy boards which are very well integrated in it and not so much elsewhere. In fact, I run Teensyduino which is a fork of the IDE that does Teensy and all the other boards;
 

Thread Starter

LikeTheSandwich

Joined Feb 22, 2021
206
It is definitely more complex, but the VSCode editor is much better than the Arduino IDE. I still use the Arduino IDE though. Mostly because the learning curve for VSCode hasn't been worth it to me yet. But because I want to use Electron, and have some Javascript stuff I will be doing for the web, I want to learn it anyway. Then I will see if I prefer it to the Arduino IDE.

Oh, and the other reason I use Arduino IDE is for Teensy boards which are very well integrated in it and not so much elsewhere. In fact, I run Teensyduino which is a fork of the IDE that does Teensy and all the other boards;
Yeah if I did lots of other programming types having a single IDE might well be worth the learning curve. But I do Arduino compatible MCUs exclusively. I bought one by accident (Particle Core, formerly Spark Core) that isn't Arduino compatible (it was only $2 or $3 thankfully) and I'm probably just never going to end up using it. I do like how all the libraries are added to a project though so it's completely self-contained in VS, that makes stuff much more portable. But again, that mainly only matters for distribution or other professional scenarios. I use one computer for everything, so I don't need that sort of portability.
 
Top