Pages: [1]   Go Down
Author Topic: Understanding calculation speed of different functions  (Read 740 times)
0 Members and 1 Guest are viewing this topic.
The Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

For a timing-critical project I am trying to understand how long it takes Arduino to carry out certain functions. One of the tests that I did involved a counter within a simple conditional statement to see how often that statement can be carried out in a second. I noticed that there is a large difference between using the 'while'-statement and the 'if'-statement. The first one seems to carry out twice as fast as the second.

The 'while'-code:

Code:
unsigned long timer = 0;
unsigned long counter = 0;

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

void loop() {
  while (timer < (millis() - 1000)){
        counter++;
  }
  Serial.print("Speed of calculation: ");
  Serial.print(counter);
  Serial.println(" Hz"); 
}

And the almost identical 'if'-code:

Code:
unsigned long timer = 0;
unsigned long counter = 0;

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

void loop() {
  if (timer < (millis() - 1000)){
        counter++;
  }
  else {
  Serial.print("Speed of calculation: ");
  Serial.print(counter);
  Serial.println(" Hz");
  }
}

Results:
While-code: 269669 Hz;
If-code: 134835 Hz

Difference 134835 / 269669 = 0.5

Any idea why the if-statement takes twice as long?
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 443
Posts: 23835
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The while stays in its loop until the end condition is met.
The if checks, performs, goes to the top of loop, checks, performs, etc.

while will appear to be stuck in the loop, the if will let other things occur.
think delay vs blink-without-delay
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's because the while loop does nothing but count until the timer expires and the if runs the entire loop() function until the timer expires.  The if is slowed down by the time it takes to exit and re-enter the loop() function.

Note that you are also measuring the time it takes to call millis() and calculate if the time has expired.

Rather than running the loop for one second you could run the loop for a million counts and compare the start and end times.  This would be two calls to millis() instead of 130,000 calls.

Code:
unsigned long startTime, endTime;
unsigned long counter = 0;

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

void loop() {
  startTime = millis();
  while (counter < 1000000) counter++;
  endTime = millis();

  Serial.print("Speed of calculation: ");
  Serial.print(1000000000UL / (endTime - startTime));
  Serial.println(" Hz");  
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: