Go Down

Topic: Which one code is faster? (Read 635 times) previous topic - next topic

Mastino

Hello everybody,
Like always very short question..:) Which one code will work faster? The second one?
Code: [Select]

void loop() {
digitalWrite(1, HIGH); 
digitalWrite(2, HIGH); 
digitalWrite(3, HIGH); 
digitalWrite(4, HIGH); 
digitalWrite(5, HIGH); 
digitalWrite(6, HIGH); 
digitalWrite(7, HIGH); 
digitalWrite(8, HIGH); 
digitalWrite(9, HIGH); 
digitalWrite(10, HIGH); 
digitalWrite(11, HIGH); 
digitalWrite(12, HIGH); 
digitalWrite(13, HIGH); 
delay(1000);
digitalWrite(1, LOW); 
digitalWrite(2, LOW); 
digitalWrite(3, LOW); 
digitalWrite(4, LOW); 
digitalWrite(5, LOW); 
digitalWrite(6, LOW); 
digitalWrite(7, LOW); 
digitalWrite(8, LOW); 
digitalWrite(9, LOW); 
digitalWrite(10, LOW); 
digitalWrite(11, LOW); 
digitalWrite(12, LOW); 
digitalWrite(13, LOW); 
delay(1000);
}


Code: [Select]

for(i=0;i<14;i++)
digitalWrite(i, HIGH); 
delay(1000);
for(i=14;i>=1;i--)
digitalWrite(i, LOW);


P.S. I wrote this code only for better understanding. So it may be wrong, bet the main idea is clearly to see.
Thank you very much!

mmcp42

1 will be a fraction faster as it doesn't have the overhead of the for loop
2 is smaller

they are different
2 sets pin 0 HIGH!!
both set pins HIGH in the same order
2 sets the pins LOW in reverse order
there are only 10 types of people
them that understands binary
and them that doesn't

Mastino

So the first one is faster. Thank you very much!

mromani

#3
Feb 02, 2012, 02:57 pm Last Edit: Feb 02, 2012, 04:05 pm by mromani Reason: 1
Had this experimental code lying around...

HTH

Code: [Select]

// code speed test

unsigned const long RUN_CYCLES = 50000;
unsigned long startTime;
unsigned long endTime;
unsigned long avgTime = 0;
float avgTime_f = 0.0;

typedef void (*testFunction)(void);


void f1() {
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);
    digitalWrite(13, HIGH);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);
}


void f2() {
    unsigned short i;
   
    for (i = 2; i <=13; i++) {
        digitalWrite(i, HIGH);
    }
    for (i = 2; i <=13; i++) {
        digitalWrite(i, LOW);
    }
}


testFunction testFunctions[] = { f1, f2 };

#define ARYLEN(a) (sizeof(a)/sizeof(a[0]))


void setup() {
    unsigned short i;
    unsigned int k;
   
    Serial.begin(9600);
    delay(1000);
    Serial.print("Testing ");
    Serial.print(ARYLEN(testFunctions));
    Serial.print(" function(s) for ");
    Serial.print(RUN_CYCLES);
    Serial.println(" cycles...");
   
   
    for (i = 0; i < ARYLEN(testFunctions); i++) {
        Serial.print("Function ");
        Serial.print(i);
        Serial.print("... ");
       
        avgTime = 0;
       
        for (k = 0; k < RUN_CYCLES; k++) {
            startTime = micros();
            (*testFunctions[i])();
            avgTime += micros() - startTime;
        }
       
        avgTime_f = (float)avgTime / (float)RUN_CYCLES;
       
        Serial.print(avgTime_f, 8);
        Serial.println(" us/call");
    }
}
   
void loop() {
}


Results:

Code: [Select]

Testing 2 function(s) for 50000 cycles...
Function 0... 108.98735809 us/call
Function 1... 115.27111816 us/call


edit: was ms/call, should have been instead us/call (microseconds)

Mastino


ea123

Hi,
If you need high speed digital output on several channels you can access directly
the registers as illustrated here: http://www.arduino.cc/playground/Learning/PortManipulation
instead of using the digitalWrite function.


MarkT


So the first one is faster. Thank you very much!


Only by a few percent, digitalWrite is a slow routine.

Perhaps you meant to ask "how do I do fast pin I/O?" to which the answer is direct-port manipulation.
[ I won't respond to messages, use the forum please ]

Mastino

Actually my question was - works the code faster if I use "for" function. You know there are a lot of ways to write the same code.
 

Go Up