Arduino: faster alternatives to digitalread() and digitalwrite()?
Access the digital ports directly!

http://www.billporter.info/ready-set-oscillate-the-fastest-way-to-change-arduino-pins/


The 3 methods I tested were

digitalWrite(pin, LOW);         digitalWrite(pin, HIGH);
CLR(PORTB, 0) ;     SET(PORTB, 0);
PORTB |= _BV(0);                   PORTB &amp;= ~(_BV(0));


[...]


As you can see, digitalWrite takes around 56 cycles to complete, while direct Port addressing takes 2 cycles. That’s a big difference in time for programs that have lot’s of IO operations!