Performance: Shall I cache the output of the millis() function?

Hi again,

I am curious how the millis() function works in Arduino and what it compiles to, however I couldn't find any good info on this (suggestions apretiated)

Is it a good idea to cache the output of millis? For exampe if I m checking it in a number of places inside the loop - would it be better to store it in a variable and just check that instead of calling the millis() function multiple times?

Or am I going to be creating extra overhead with and additional variable? (i.e. does the milis() map directly to the value in compiled code and it doesn't matter that it looks like a function call in Arduino?)

I usually save millis() to a variable at the start of loop() - mainly so that all the decisions are based on the same value in that iteration of loop().

I can't imagine there is a performance concern unless you want some very tight timing.

It would not take you long to write a short program to measure the "cost" of reading millis()

...R

Here is millis() at line 65 :
https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/wiring.c
It is a short function, but using "unsigned long currentMillis = millis() ;" at the begin of the loop() is also common, as Robin2 mentioned.

Peter_n:
Here is millis() at line 65 :
https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/wiring.c
It is a short function, but using "unsigned long currentMillis = millis() ;" at the begin of the loop() is also common, as Robin2 mentioned.

OK, thanks, appreciate your help!