Does Arduino code break more functions / resources into functions?

Let's say I have this code below:

int led = 13; void setup() { pinMode(led, OUTPUT); } void loop() { ledChange(HIGH); delay(1000); ledChange(LOW); delay(1000); } void ledChange(int pinState) { digitalWrite(led, pinState); } 

Whether moving digitalWrite(led, pinState); in its own function affects processing time?

Of course, performance does not matter when using a single LED, but it can matter when each cycle cycle is counted (high sampling rates, many mathematical cycles, etc.).

+4
source share
3 answers

Yes, you spend a lot of time on hours. When you write ledChange(LOW) , it ledChange(LOW) into an instruction of type CALL (which tells the program counter register to go to the location of the method).

So this will be compiled basically:

  • Put LOW on some register or stack
  • go to location ledChange()
  • Remove led from memory and put it along with LOW somewhere
  • go to digitalWrite()
  • do whatever is in digitalWrite()
  • come back
  • come back

Note that the CALL jump involves a lot of stack clutter and takes a lot longer than the usual JMP instruction.

On the other hand, it only does digitalWrite(led,LOW) :

  • Remove the led , LOW from somewhere in the memory and put them somewhere available.
  • go to ditigalWrite()
  • do whatever is in digitalWrite()
  • come back

I'm not quite sure how the arguments are passed in the corresponding compiled code, this is probably part of the call. Also note that compilers are changing, and some are smarter than others.

You can see that your encapsulation of the function caused the program to take more clock cycles each time you run it. However, this is not worth optimizing; I do not see a large capacity in such encapsulation to slow down Arduino. In addition, as I already mentioned, some compilers optimize such things.


This has nothing to do with the function being void . If it was an int function, it would be a little slower, since saving an int before returning includes an MV operation or a stack (forgot that).

+7
source

A function defined as void simply informs the compiler / optimizer that the return value expected from the function is not expected.

Thus, no code will be created to store or manipulate any return value.

This is not specific to Arduino, this is the common behavior of C.

+8
source

You can configure avr-gcc to display the assembly. Having figured this out, you can see that the type of the returned function does not matter with respect to the size of the code. If you do not return any value, it will not generate additional code. However, you will get compiler warnings, so it’s nice to use void in this case.

If you use functions, you will likely have a short code, because you do not need to repeat the code in the function every time you need it (this means that you will need to have enough code in the function to shift calls and return instructions, which is almost always takes place). This will make the code a little slower as it needs to follow the call and return instructions, however this will most likely be offset by a decrease in size and better code maintenance.

Hope I was clear enough, this seems a bit confusing :)

+2
source

All Articles