I'm very new to embedded programming, and been doing lots of reading on the subject. Much to my surprised, I've recently heardthat a С function call requires quite a few clocks to process, as C saves and restores all sorts of registers when compiling to assembly (I heard the number 70-100 thrown out in a video from circa 2012, so it may be dated or refer to an older compiler. My own playing with the simulator suggests it's more like 30-40). I'm wondering how well optimization handles this.
I'm used to writing modular code - breaking things up as functions, for ease of use and maintainability. But if that comes with this sort of overhead, it may not be worth it, especially on code that needs to run fast. Doesn't much matter on a modern PC, but on an 8-bit micro (my "standard" micro right now is the ATMega328p running at 8MHz), especially one running some sort of time constrained loop, it can be critical.
So I wonder how well modern avr-gcc optimizes code. For example, I often write functions like this:
void foo() { initialize(); . . . //do stuff . . . cleanUp(); } void initialize() { //do start up tasks } void cleanUp() { //do clean up tasks }
In an example like this, the initialize() and cleanUp() functions are only ever run from foo(), and only run once each. There's no reason not to include that code in foo() itself, except that I like to keep things broken up this way for neatness. More style than anything.
Is the compiler smart enough to optimize away the function calls and collapse it into one function? Would marking them "inline" help? Or should I just change my style around this?
Thanks.