Pages: [1]   Go Down
Author Topic: Which one code is faster?  (Read 574 times)
0 Members and 1 Guest are viewing this topic.
Baltic states
Offline Offline
Jr. Member
**
Karma: 0
Posts: 97
new day, new try
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everybody,
Like always very short question..smiley Which one code will work faster? The second one?
Code:
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:
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!
Logged

Leighton Buzzard, UK
Offline Offline
Edison Member
*
Karma: 21
Posts: 1339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

there are only 10 types of people
them that understands binary
and them that doesn't

Baltic states
Offline Offline
Jr. Member
**
Karma: 0
Posts: 97
new day, new try
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
God Member
*****
Karma: 2
Posts: 596
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Had this experimental code lying around...

HTH

Code:
// 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:
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)
« Last Edit: February 02, 2012, 10:05:28 am by mromani » Logged

Baltic states
Offline Offline
Jr. Member
**
Karma: 0
Posts: 97
new day, new try
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow! Nice! Thanks a lot!
Logged

Offline Offline
Jr. Member
**
Karma: 4
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

0
Offline Offline
Shannon Member
****
Karma: 201
Posts: 11744
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

[ I won't respond to messages, use the forum please ]

Baltic states
Offline Offline
Jr. Member
**
Karma: 0
Posts: 97
new day, new try
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
 
Logged

Pages: [1]   Go Up
Jump to: