Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does breaking Arduino code into functions take up more space/resources?

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);
}

Does moving digitalWrite(led, pinState); to its own function affect processing time?

Of course, performance doesn't matter with a single LED, but it could matter when every clock cycle counts (high sampling rates, lots of math in loops, etc.).

like image 464
Anonymous Penguin Avatar asked Apr 10 '13 22:04

Anonymous Penguin


2 Answers

Yes, you are wasting a teensy amount of clock cycles. When you write ledChange(LOW), it is compiled to a CALL type instruction (which tells the program counter register to jump to the location of the method).

So, this will basically compile to:

  • Put LOW in some register or the stack
  • jump to the location of ledChange()
  • Fetch led from memory, and put it along with LOW somewhere
  • jump to digitalWrite()
  • do whatever is in digitalWrite()
  • jump back
  • jump back

Note that a CALL jump involves a lot of messing with the stack, and takes a lot longer than a regular JMP instruction.

On the other hand, doing just digitalWrite(led,LOW) will do:

  • Fetch led,LOW from somewhere on the memory and put them somewhere accessible
  • jump to ditigalWrite()
  • do whatever is in digitalWrite()
  • jump back

I'm not entirely sure how arguments are passed in the corresponding compiled code, it's probably a part of the call. Also note that compilers vary, and some are smarter than others.

You can see that your encapsulation of the function made the program take up more clock cycles every time you while running it. However, this isn't really worth optimizing; I don't see much capacity in such encapsulation to slow down the Arduino. Besides, like I mentioned, some compilers optimize such things.


This has nothing to do with the function being a void. If it was an int function, it would be ever-so-slightly slower since storing the int before return involves a MV or stack operation (forgot which).

like image 119
Manishearth Avatar answered Nov 15 '22 06:11

Manishearth


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

There will thus be no code generated for saving or manipulating any return value.

This is not Arduino specific, it is a generic C behavior.

like image 28
Anindo Ghosh Avatar answered Nov 15 '22 07:11

Anindo Ghosh