void loop speed

What determines a void loop´s speed?

i am making a project where the loop needs to be as fast as possible

I call functions from voids that is outside the loop. when these functions is NOT called, does they make the loop slower? or does they only affect the speed of the loop when they ARE called? I want to have the smallest possible loop so i use voids that is outside the loop to make it run fast, when they are not called. Is this approach wrong?

It's called functions, not voids :wink: If you don't call functions, loop will be faster; the empty loop will be the fastest you can get but is obviously not very useful :smiley:

Calling functions cost a little more time as the processor has to do some things
save some data
call the function
return from the function
restore the saved data

Be aware that we're talking about a few microseconds extra for the above. You can use inline functions; it will at least 'prevent' the call and return.

So now the question is why you think you need the fastest possible loop?

loop is called continually - as soon as it gets to the end of loop, it calls loop again. So how fast loop is depends almost entirely on the code you put in it (there's around... I think it's half a microsecond of overhead per call - this is rarely an issue).

Calling sub-routines from loop to deal with various aspects of the programming is good practice - it makes it easier to read and modify the code - but it does not improve execution speed. As sterretje noted, it may introduce a small amount of overhead, though the compiler is probably smart enough to optimize that out.

As godivaPrima suggested, I recommend you to write some code to test the loop speed and run some experiments to see the effect of different things on loop speed. There will be some overhead added for the test but this should be consistent from test to test so as long as you are only comparing the difference between the test results, rather than the absolute values, it shouldn't matter. You can easily write some code to do this or I wrote a simple library for it because I was tired of doing it over and over again:

What may often cause unexpected results is that the compiler is doing a lot of optimization of your code but optimization is relative to your goal. Arduino AVR Boards uses the -Os compiler flag, which optimizes for the smallest program size, rather than the fastest speed. Arduino AVR Boards 1.6.12 and newer are even smarter about optimization due to enabling LTO(link time optimization). For example, previously the setup and loop functions weren't able to be inlined, causing overhead for the function calls, but with the recent versions those functions are now inlined and there's no overhead.

One trick I recently picked up courtesy of one of our faithful forum admins, Coding Badly, is that you can speed up the loop a tiny bit by adding the following line to your sketch:

void serialEventRun() {}

Result of unmodified SpeedTestExample.ino on Arduino Uno, Arduino AVR Boards 1.6.15 = 3.14 microseconds
Result of SpeedTestExample.ino with empty serialEventRun() definition on Arduino Uno, Arduino AVR Boards 1.6.15 = 2.14 microseconds

That's right, a whole microsecond faster! Woohoo. I'm sure there are opportunities for much greater speed increases in most sketches but this is something very easy to do and will also reduce the flash usage of your sketch.

The reason is that serialEventRun() is called on every loop. The function is weakly defined in the core so by adding an empty definition to your sketch it overrides the stock definition and the blank function is optimized away by the compiler. The drawback of this is that you can no longer use the serialEvent() feature in sketches with the empty definition but since that is very rarely used it's no loss for the vast majority of sketches.

In the end, focusing too much on optimization can just lead to unnecessary confusion for a beginner and it's better to not worry so much about it until you run into a situation where a slow loop is actually causing you problems. If you are using delay or other blocking code then putting a bunch of effort into shaving a few microseconds off your loop is pretty much pointless.

i am making a project where the loop needs to be as fast as possible

Tell us what the project is and what interval (microseconds) you require between repeats and we will be able to give much more useful information.