mercredi 28 juillet 2021

Does evaluating 'else if' take as much CPU time as evaluating 'if' in C?

Initial assumption: We will run the following programs 1000 times with argv[1] not being NULL :

#include<stdio.h>
#include<stdlib.h>


int main( int argc, char *argv[] )  {
 
 if ( argv[1] == NULL ) {
  printf("Usage: a.out argument1");
  exit(1);
}
 else if ( argv[1] != NULL ) {
    
    some_expensive_computation;
    
   
   }

return 0;   
}

Will the above code run faster in such case than the following:

#include<stdio.h>
#include<stdlib.h>


int main( int argc, char *argv[] )  {
 
 if ( argv[1] == NULL ) {
  printf("Usage: a.out argument1");
  exit(1);
}
 if ( argv[1] != NULL ) {
    
    some_expensive_computation;
    
   
   }

return 0;   
}

I've already learned that the fastest code would make use of else instead of else if. Does evaluating 'else if' take as much CPU time as evaluating 'if' in C?

Aucun commentaire:

Enregistrer un commentaire