# cycle time

May be a dumb question, but how long is a “Loop”{}?

It must be affected by sketch length and complexity?

Is there some way to find out how long a specific sketch takes to run thru? In other words, the total cycle time?

Thanks.

It’s as long as it needs to be. If you ask it to do more work, it’s going to take more cycles. You can wrap your loop code into a timing harness, if you like. Convert:

``````void setup()
{
[glow]my old setup code here[/glow]
}

void loop()
{
[glow]my old loop code here[/glow]
}
``````

into:

``````void loop()
{
long start = millis();
static long count = 10;
for (long i = count; i > 0; i--)
my_loop();
long end = millis();
Serial.print("Took ");
Serial.print(end - start);
Serial.print("ms to loop ");
Serial.print(count);
Serial.println(" times.");
count *= 2;
}

void setup()
{
Serial.begin(9600);
[glow]my old setup code here[/glow]
}

void my_loop()
{
[glow]my old loop code here[/glow]
}
``````

You’ll get timing studies on your loop.

Took 0ms to loop 10 times.
Took 0ms to loop 20 times.
Took 2ms to loop 40 times.
Took 5ms to loop 80 times.
Took 11ms to loop 160 times.
Took 22ms to loop 320 times.
Took 43ms to loop 640 times.
** :**

Run it long enough to be satisfied you’re getting a good numerical result (like 1000ms or more). Then divide by the count, and you have the time taken by one loop() call.

This is good info, thanks.

Curious, are the times you posted from an actual run? If so, are they from the code you listed which appears to be about 10 lines?

I only need a gross estimate to get a rough idea how fast my sketch (does not exist yet!) will take to loop.

No, they’re times I made up. They are only timing the duration of the code represented by my old loop code here. At 16 MHz, that’s 16 clock ticks per microsecond. A millisecond is 16000 clock ticks, plenty of time to get a little work done.

Note that you can have your loop() function act like a traditional “main()” and not actually loop (never “return”)

The Arduino “core” that calls loop IS:

``````int main(void)
{
init();
setup();
for (;;)
loop();
return 0;
}
``````

So it’s not doing anything for each loop to slow down … anything. For all practical purpose, the only code that takes time to run is YOUR code.

im gonna throw yet another noob-bomb onto this forum…

im new to coding, atleast using the c library that is…

i wanted to knw, every time i declare a “void whatever”, am i declairng my own function, or class, or what… cause like in ure code u have the standard void loop and void setup, then u have void_mysetup, and i just dont understand how that plays into the entire code? does it run parallel to the other parts of the code, or what?

very confused, pls help smart people!!! :-[

“void name()” creates a function. void means that it doesn’t return any value (some languages call a function that doesn’t return any value a “subroutine” to distinguish, but C calls everything a function.)
It runs when some other code ‘calls’ it. NOTHING runs in parallel on a microcontroller (although things that use interrupts may appear to do so.)