-7

Which of the following work faster?

  • Program 1

Using register

int main(){
   register int i;

   for(i=0;i<=100;i++)
       printf("%d\n",i);

   return 0; 
} 
  • Program 2:

Using auto

int main(){    
    auto int i;

    for(i=0;i<=100;i++)
        printf("%d\n",i);

    return 0;
} 
Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74
Enkesh Gupta
  • 37
  • 2
  • 9

2 Answers2

2

Most likely there is no difference. The register keyword is to specify the compiler to store the variable in the CPU register, however most modern compilers do this automatically.

As for the auto keyword, it is redundant, because it there by default. So

float b;

will be the same as

auto float b;

However on such a small example it is practically impossible to see which one is faster and more intensive tests are required.

A B
  • 497
  • 2
  • 9
  • 1
    That is why it was a gentle suggestion from my side. Don't worry, My English speaking skills are horrible too, but sometime, just I come up with some good words, that's all. :-) – Sourav Ghosh Jun 29 '15 at 07:39
1

Wiser heads will give more details, but basically your register int isn't going to do anything. The compiler will do what it thinks is faster, and I'll bet the same way in both cases.

You would have to be writing some extremely low-level code to make register variables make sense, since they are literally using CPU registers directly.

moffeltje
  • 4,521
  • 4
  • 33
  • 57
Engineer
  • 834
  • 1
  • 13
  • 27