What design is more efficient?

int a, n = 10; for(a = 0; a < n; a++) { //......... } 

 int n = 10; for(int a = 0; a < n; a++) { //......... } 

My opinion is that if a cycle is used once, then it is better to declare (int a), and if it’s several times, then just (a) everywhere. I think connected with memory, help me figure it out.

  • @risonyo, To format the code, select it with the mouse and click on the button 101010 of the editor. - Deleted

2 answers 2

Strictly speaking, there is no difference, unless the counter designer does some huge calculations (99.9%).

And even if it does, then such small optimizations should be done by compilers. If you sort the bubble in the loop, then you will not see the speed increase exactly ;-)

+ Such an approach will create an additional global variable, which is not necessary, which is not very good.

I will say this: do not bother with similar things, this is not the code that needs to be optimized.

ps Premature optimization is a serious sin.

  • one
    Even if a is declared globally (although, I'm sure, risonyo was going to locate it locally), but used only once, as a loop counter, the compiler will most likely make it a register variable. Premature optimization is, of course, evil, but certain recommendations (not necessarily related to optimization) should be followed (about the same global variables). - insolor

If you use the int type. And the for cycles themselves will be over 10,000,000. So here

 for (10000000) { for (a = 0; a < n; a++) { } // это быстрее for (int a = 0; a < n; a++) { } // это медленнее } 

In other cases, the difference is nanoseconds. And to spend on this time is meaningless and you should always use for (int a = 0; a < n; a++) .

  • one
    Believe me, unless some kind of antediluvian compiler is used, there will be no difference between these cycles. Moreover, if there is no loop body, the compiler can just throw both, and in this case the first one will be longer. Why? but just in the second case, it is clear that the variable a not used at the exit from the cycle, its value is not important. The first will have to do one assignment. - KoVadim
  • one
    It’s unethical to talk about the effectiveness of the code and not to talk about how the compiler works (at least in general terms). It’s like a spherical horse in a vacuum. - KoVadim
  • 2
    >> But he did not say which compiler. And the options are actually not so much. gcc, msvc, clang, intel c ++, although it usually ends with the first two. >> will be created not in the RAM, but in the processor cache? Those sections of RAM that are often used get into the cache. And just to create it in the cache does not work. Another thing is, if this variable is not used inside a loop, and the loop itself is small enough, then the compiler can simply use the register of the processor - but addressing the register of the processor is really very fast. - KoVadim
  • 2
    In general, write the code easier. Need a variable - declare. If the code has thirty cycles one by one and each has its own variable, then nothing bad will happen (well, except for loss of readability). Modern compilers optimize much simpler simple code than "sly hand-optimized". - KoVadim
  • 2
    It is advisable to use the same variable for all cycles. for (int i = 0; i <n; i ++) {} for (int i = 0; i <n; i ++) {} for (int i = 0; i <n; i ++) {} for (int i = 0; i <n; i ++) {} not to get confused for (int i = 0; i <n; i ++) {} for (int j = 0; j <n; j ++) {} for (int w = 0; w <n; w ++) {} for (int q = 0; q <n; q ++) {} - manking