Which one code is faster?

Hello everybody,
Like always very short question..:slight_smile: Which one code will work faster? The second one?

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);
}
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!

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

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

Had this experimental code lying around...

HTH

// 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:

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)

Wow! Nice! Thanks a lot!

Hi,
If you need high speed digital output on several channels you can access directly
the registers as illustrated here: Arduino Playground - PortManipulation
instead of using the digitalWrite function.

Mastino:
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.

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.