if or modulo, what is fastest on AVR?
i building binary alarm clock , wonder fastest use in code, modulo , divide or if's.
i used same model here timekeeping:
as can see uses lot's of % , /. because it's faster or because it's easier read? maybe it's slower modulo (and divides), according this post, modulo quite slow.
for example, there big difference in clock cycles used these 2 examples saving end-time of 10min snooze-period (current time + 10 minutes)?:
maybe better knowledge me on avr processors can answer?
i used same model here timekeeping:
code: [select]
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 can see uses lot's of % , /. because it's faster or because it's easier read? maybe it's slower modulo (and divides), according this post, modulo quite slow.
for example, there big difference in clock cycles used these 2 examples saving end-time of 10min snooze-period (current time + 10 minutes)?:
code: [select]
//modulo alternative:
if(buttonpressed)
{
snoozehours = hours;
snoozehours += (minutes+10)/60; // if snooze minutes more 60 add 1 hours
snoozeminutes = (minutes+10)%60; // , substract 60 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 better knowledge me on avr processors can answer?
my first thought not need worry if need resolution of 1 second.
then have plenty of time process (other) things.
sorry not answering question.
then have plenty of time process (other) things.

sorry not answering question.
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > if or modulo, what is fastest on AVR?
arduino
Comments
Post a Comment