Pages: [1]   Go Down
Author Topic: Cycles per second in the loop()  (Read 1865 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 360
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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."
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Guadalajara, Jalisco, Mexico
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 208
Posts: 12936
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Guadalajara, Jalisco, Mexico
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 208
Posts: 12936
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Global Moderator
Online Online
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

This sketch, for example:

Code:
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.
Logged


Global Moderator
Online Online
Brattain Member
*****
Karma: 485
Posts: 18810
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Pages: [1]   Go Up
Jump to: