Cycles per second in the loop()

How many times per second does the loop() run?

I realize this will vary by board, but lets use the Arduino Uno (ATmega328 SMD Edition) as example.

I'd like to know how to look this up for each board, if it is in fact different as one would suspect.

1 Like

loop() is not timed. The number of loops in a second is equal to 16000000 divided by the number of processor cycles your loop() method takes - if the loop() is empty, it will run at 16MHz, whereas if it has 32000000 processor cycles it will run at 0.5Hz.

Timing is most easily accomplished with Arduino's timing methods, as seen in the BlinkWithoutDelay sketch. You can also use hardware timers (MsTimer2 and FlexiTimer2 are both good libraries for this) to trigger an ISR at specific intervals, although there are some other caveats with it.

I realize this will vary by board, but lets use the Arduino Uno (ATmega328 SMD Edition) as example.[/quote]

While technically true, most of the Arduino boards run 16MHz. Since they are all based on the same core microcontroller,they execute instructions at the same speed. So this really isn't a differentiator.

The answer to your question is "depends on the code." and to save some time follow-up questions like "what is the fastest I can ...." or "what is the fastest code to ...." Those are ridiculous to speculate on.

Instead, ask what you are trying to accomplish, and please ask in a proper forum. This question has nothing to do with "Installation or Troubleshooting."

I know this is a really old topic, but I searched for this on google and this topic was a top 5 result and I though someone else might be wondering the same, so I made a code example to count the number of iterations per second (loops per second).

I'm getting ~84,200 loops per second on my ArduinoMega 2560.

Hope this helps someone out there.

long lastMillis = 0;
long loops = 0;

void setup(){
  Serial.begin(9600);
}

void loop(){
  long currentMillis = millis();
  loops++;
  
  /* By doing complex math, reading sensors, using the "delay" function,
  *  etc you will increase the time required to finish the loop,
  *  which will decrease the number of loops per second.
  */

  if(currentMillis - lastMillis > 1000){
    Serial.print("Loops last second:");
    Serial.println(loops);
    
    lastMillis = currentMillis;
    loops = 0;
  }
}
  if(currentMillis - lastMillis > 1000){
    Serial.print("Loops last second:");

Shouldn't the message read "Loops last 1.001 seconds:"?

Yes it should, but for simplicity purposes I decided to leave it like that, and actually, it would have to say: "Loops last 1.001 to 1.003 seconds" (Some times it might take more than 0.001 second to complete the loop).

Anyway, leaving it like that gives you ~99.9% accuracy, which I think is good enough.

Its a very basic question so I gave a basic and concise answer :grin:

Instead of correcting the message, you could always correct the comparison.

It's a meaningless figure. Even measuring how long loop takes, makes it take longer.

This sketch, for example:

void setup ()
  {
  pinMode (13, OUTPUT);
  }  // end of setup

void loop ()
  {
  PINB = _BV (5);  // toggle pin 13  
  }  // end of loop

Putting a logic analyzer on pin 13, I get a period of 875 nS (14 clock cycles) between toggles. Thus loop in this case is executed 1/875e-9 (1,142,857) times per second.

1 Like

Since it took 2 clock cycles to toggle the pin, I suppose you could say the "raw" time to execute loop is 1/750e-9 which is 1,333,333 times per second. However that information isn't particularly useful as, as soon as you do something, it will take longer. Plus the timer interrupts used to count milliseconds (for millis) interrupts once every 1.024 mS, slowly down loop slightly.

1 Like

Cycles per second in loop varies greatly with lots of things (platform, code etc.) however there must be a way to work around this. I modified some example code from RTClib softrtc to count the loops between output from the routine. It varies wildly but since you can enumerate each point at which it rolls over between seconds you can simply divide that raw count number by the rollover iteration and come up with a fraction of a second. I am trying to use this code to record barograph data from a bmp180 and data from a microphone array.

I am a raw beginner but the following code will give someone smarter than me a way to work it out.

I also found a crude fix for another problem and the answer and source are in the code.

This a routine that I found in the examples section of RTClib code.

I think that by watch

// Date and time functions using just software, based on millis() & timer "Softrtc"

#include <Arduino.h>
#include <Wire.h> // this #include still required because the RTClib depends on it
#include "RTClib.h"
#include <time.h>
// Was getting error collect2.exe error ld returned 5 exit status
// Fix was at many cases of ld.exe crashes. (on windows xp). - IDE 1.x - Arduino Forum
int y = 0;
int z = 0;
int w = 0;
//I was trying to figure out a way to get timing info below the 1 second threshold
//so I used one of these variables in my tests.
//First time I ran this i had y=y++ and it was kicing over each second at 1sec/1190.
//now with this new line it rolls over at 1sec/1294
//now just tried y=y+1 and it rolls over at 1 sec/674
RTC_Millis rtc;

void setup () {
Serial.begin(57600);
// following line sets the RTC to the date & time this sketch was compiled
rtc.begin(DateTime(F(DATE), F(TIME)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
rtc.adjust(DateTime(2015, 12, 03, 7, 46, 0));
}

void loop ()
{
DateTime now = rtc.now();

//Serial.print(now.year(), DEC);
//Serial.print('/');
//Serial.print(now.month(), DEC);
//Serial.print('/');
// Serial.print(now.day(), DEC);
//Serial.print(' ');
// Serial.print(now.hour(), DEC);
// Serial.print(':');
//Serial.print(now.minute(), DEC);
// Serial.print(':');
Serial.print(now.second(), DEC);
y=y+1;
//y++;
Serial.print(" /");
Serial.println(y);
Serial.print('/');
}