Skip to main content
hazall
Associate II
May 15, 2023
Question

Which flag definition would be faster ?

  • May 15, 2023
  • 1 reply
  • 840 views

I have a lot of different flags in project, I wonder which style definition reads faster by processor .

Definition 1 
 
 
 
 typedef struct{
 
 __IO uint8_t flag1 ;
 
 . //other flags
 
 
 
}Flag_t ;
 
 
 
Definition 2 
 
 
 
 typedef struct{
 
 __IO uint32_t flag1 : 1 ;
 
 //other flags
 
 .
 
}Flag_t ;
 
 
 
Definition 3
 
 
 
 typedef struct{
 
 __IO bool flag1
 
 . //other flags
 
 
 
}Flag_t ;
 
 
 
 
 
Flag_t Flag;
 
 
 
int main(){
 
if(Flag.flag1&(bit masking) == SET){
 
//something..
 
}
 
}
 
 
 
 

    This topic has been closed for replies.

    1 reply

    Nikita91
    Lead II
    May 15, 2023

    IMO the use of uint8_t and bool are equivalent and fast. The size of bool is 1 byte. The assembly instruction LDR can load the byte and extend to 32 bits in one operation.

    The use of bit fields is slower if you use more than 1 definition in the same int. If you use only 1 bit in the int the compiler may optimize and revert to the uintX_t use.

    Compile your code then inspect the ***.list file in Debug folder to see what is generated by the compiler. Most of the time the fewer instructions the faster the code.