simplest speed test

14 Apr 2010

Here is the Arduino code  toggling an IO pin. It takes 400ns.

PORTD |= 0x04;
PORTD &= 0xFB;

Here is the mbed code toggling an IO pin. It takes 645ns. (longer than Arduino)

pin5 = 1;
pin5 = 0;

It's not completely fair. Because it would certainly take longer if one uses DigitalWrite(2,1) and DigitalWrite(2,0) (the encapsulated Arduino function calls) to toggle IO pin, which is equivalent to the instance method in mbed code.

Then it leads to my question: is there a direct register accessing method to make mbed toggle IO pin faster?

14 Apr 2010

Check my investigation of this.

15 Apr 2010

Great. Here is the register access method for mbed. It takes 83ns to toggle an IO pin. About 5 times faster than Arduino.

 

#include "mbed.h"

#define TEST_1 (1<<9)


int main() {
    LPC_GPIO0->FIODIR |= TEST_1;
    
    while(1) {
    
        LPC_GPIO0->FIOPIN |= TEST_1;             
        LPC_GPIO0->FIOPIN &= !(TEST_1);          

    }
}

15 Apr 2010

If you replace the loop body by:

LPC_GPIO0->FIOSET = TEST_1;
LPC_GPIO0->FIOCLR = TEST_1;

it should become even faster.