I looked at what the compiler does for statements like these:
Code: Select all
if ( GPIOB->IDR & 1 << 10 ) ...;
if ( GPIOB->IDR >> 10 & 1 ) ...;
I thought that potentially the first of those might execute a tad faster than the second, but no, it turns out the compiler is smart enough to generate exactly the same code, like this, with the shift effectively done at compile time for both of those:
Code: Select all
0x08005d7c: 0d 4b ldr r3, [pc, #52] ; point to GPIOB
0x08005d7e: 1b 69 ldr r3, [r3, #16] ; grab IDR
0x08005d80: 13 f4 80 6f tst.w r3, #1024 ; & with 1 << 10
0x08005d84: 01 d0 beq.n 0x8005d8a <setup()+102>
By contrast, using digitalReadFast(digitalPinToPinName(PB10)) generated way more instructions. Even digitalReadFast(PB_10) was about twice as many instructions as directly referring to the IDR. Still, you would have to be generating interrupts at a very high rate for this to make any difference.