Go Down

Topic: Cycles per second in the loop() (Read 2 times) previous topic - next topic

nuance

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.

Aeturnalus

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.

James C4S

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."
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Jesuso

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.

Code: [Select]

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

Coding Badly

Code: [Select]
  if(currentMillis - lastMillis > 1000){
    Serial.print("Loops last second:");


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

Jesuso


Code: [Select]
  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  :smiley-mr-green:

Coding Badly


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

Nick Gammon

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

This sketch, for example:

Code: [Select]
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.
http://www.gammon.com.au/electronics

Nick Gammon

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.
http://www.gammon.com.au/electronics

Go Up