Show Posts
Pages: [1]
1  Forum 2005-2010 (read only) / Syntax & Programs / Re: if or modulo, what is fastest on AVR? on: October 27, 2009, 11:00:44 am
Quote
I see in your code that you are disabling interrupts before calling millis, You don't need to do that, millis does that for you.
Ok, thanks for the info.

Quote
BTW, I would expect you would get different numbers if you had used bytes instead of ints for your hour, minute and seconds variables
true, perhaps I should try that too just for fun.

Quote
Also, I agree with the sentiments expressed by AB, kg and RC that its better to use what makes the most readable and logical source code and not worry about optimization until you really need to.
I agree too, however in this case I think both versions are very readable, the modulo version is certainly shorter, but the if version is easy to follow, and for a person new in programming I think the modulo version can be a little confusing at first.
2  Forum 2005-2010 (read only) / Syntax & Programs / Re: if or modulo, what is fastest on AVR? on: October 27, 2009, 09:46:37 am
I know this thread is quite old, but I have made a test now. The result was that the clockfunction with if's was 15,5 times faster than the modulo version.

i ran it 10000 times each and timed it, the modulo version took almost 1.5 seconds (ca 1470 millis) and the if version ca 95 milliseconds.

Here is the functions I used:
Code:
// CLOCK VARIABLES:
#define MAX_MILLIS_VALUE 34359738
unsigned long current_millis_value = 0;
unsigned long previous_millis_value = 0;
unsigned long m = 0;
unsigned int seconds = 0;
unsigned int minutes = 0;
unsigned int hours = 0;

void moduloClock()
{
  cli(); // disable interrupts*
  current_millis_value = millis();
  sei(); // enable interrupts*
  if (current_millis_value < previous_millis_value) // if millis overflows
  {
    m += MAX_MILLIS_VALUE - previous_millis_value + current_millis_value;
  }
  else // if millis has not overflown
  {
    m += current_millis_value - previous_millis_value;
  }
  seconds += m / 1000;
  m = m % 1000;
  minutes += seconds / 60;
  seconds = seconds % 60;
  hours += minutes / 60;
  minutes = minutes % 60;
  hours = hours % 24;
  previous_millis_value = current_millis_value;

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * *= "Also, you should disable interrupts while calling millis(),         *
 * otherwise you run the risk of getting corrupted data should the         *
 * timer0 overflow occur while millis() is performing its computation"     *
 * source: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1215338347/0#1  *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
}

/* Clock function that uses if statements instead of modulo */
void ifClock()
{
  cli(); // disable interrupts
  current_millis_value = millis();
  sei(); // enable interrupts
  if (current_millis_value < previous_millis_value) // if millis overflows
  {
    m += MAX_MILLIS_VALUE - previous_millis_value + current_millis_value;
  }
  else // if millis has not overflown
  {
    m += current_millis_value - previous_millis_value;
  }
  if (m>999) /* if m is 1000 or over  */
  {
    seconds++;
    m = m-1000;
  }
  if (seconds>59) /* if seconds == 60 */
  {
    minutes++;
    seconds = 0;
  }
  if (minutes>59) /* if minutes == 60 */
  {
    hours++;
    minutes = 0;
  }
  if (hours>23) /* if hours == 24 */
  {
    hours = 0;
  }
  
  previous_millis_value = current_millis_value;
}

Edit: fixed an error in the code and added some comments.
3  Forum 2005-2010 (read only) / Syntax & Programs / Re: if or modulo, what is fastest on AVR? on: July 14, 2009, 10:18:12 am
Thanks for all the answers, yes you are maybe right that it doesn't matter much which method I use, but there will be alot more in the loop than the clock code. I will use capacitive sensors (link) as buttons and play some melody on a piezo element when the alarm goes off etc.

I think I'll try with divisions and modulo first and rewrite if there is problems.

@RuggedCircuits, Thanks, it's always good to know how it is, if I need to, I know what i could try to optimize.

@Kitep, yeah it would be intresting to see how big the diffrence is, maybe I should do a test.
4  Forum 2005-2010 (read only) / Syntax & Programs / if or modulo, what is fastest on AVR? on: July 13, 2009, 06:21:30 pm
I am building a binary alarm clock and I wonder what is the fastest to use in the code, modulo and divide or if's.

I used the same model as here for timekeeping:
Code:
current_millis_value = millis();
  m += current_millis_value - previous_millis_value;
  seconds += m / 1000;
  m = m % 1000;
  minutes += seconds / 60;
  seconds = seconds % 60;
  hours += minutes / 60;
  minutes = minutes % 60;
  hours = hours % 24;
  previous_millis_value = current_millis_value;

As you can see it uses lot's of % and /. Is this because it's faster or just because it's easier to read? Maybe it's actually slower with modulo (and divides), according to this post, modulo is quite slow.

For example, is there a big difference in clock cycles used for these two examples that are saving the end-time of the 10min snooze-period (current time + 10 minutes)?:

Code:

//modulo alternative:
if(buttonPressed)
{            
    snoozeHours = hours;
    snoozeHours += (minutes+10)/60; // if snooze minutes is more than 60 add 1 to hours
    snoozeMinutes = (minutes+10)%60; // and substract 60 from the snooze minutes
}

// if alternative:
if(buttonPressed)
{      
      if(minutes<50)
      {
          snoozeMinutes = minutes+10;
          snoozeHours = hours;
      else
      {
         snoozeMinutes = minutes-50; // or minutes+10-60
         snoozeHours = hours+1;
      }
}
  

Maybe somebody with better knowledge than me on AVR processors can answer?
5  Forum 2005-2010 (read only) / Syntax & Programs / Re: Order of functions matters? on: July 04, 2009, 07:25:29 pm
No I didn't, but I noticed that I had a old version of the arduino software, so I downloaded the newest one and then it works without modification. So maybe declaring functions wherever you wan't is a feature added in a newer version. Anyway, thanks for all the help.
6  Forum 2005-2010 (read only) / Syntax & Programs / Order of functions matters? on: July 04, 2009, 08:28:20 am
When I try to compile this example I had to put the freqout() function before loop(), otherwise I got the error:
Code:
In function 'void loop()':
error: 'freqout' was not declared in this scope
but according to the arduino reference the order shouldn't matter. Then why does it?


7  Forum 2005-2010 (read only) / Exhibition / Re: Arduino as Capacitive Sensor on: June 29, 2009, 08:11:36 am
What is the difference between this and http://www.arduino.cc/playground/Code/CapacitiveSensor? Is the one wire method only for direct touch, while this can sense a longer distance?
8  Forum 2005-2010 (read only) / Bar Sport / Re: Hello all on: June 29, 2009, 08:11:20 am
Hello!

same reason
Pages: [1]