instance
instance

Reputation: 1374

Is it more efficient when counter of a for-loop is declared inside "for" statement?

int i=0;
for(i=0;i<SIZE;i++)
{
   //code
}

for(i=0;i<SIZE;i++)
{
   //code
}


//Second Way

for(int i=0;i<SIZE;i++)
{
    //code
}

for(int i=0;i<SIZE;i++)
{
    //code
}

This looks a silly question, but i am confused which one should I use in my coding. I am asking this question with reference to memory storage impact or anything else.

Upvotes: 1

Views: 146

Answers (7)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145279

There is no difference in efficiency of execution, but there's a big difference in efficiency of programming.

Using the same variable for two or more purposes inevitably leads to incorrect expectations about the variable's value, forcing you to hunt for bugs, thus inefficiently wasting your time.

As a general rule, restrict a variable's scope as much as practically possible. This helps both with the problem of "reusing" a variable, and with e.g. having the necessary information available when an object is constructed, avoiding the need to update.

Upvotes: 1

IllusiveBrian
IllusiveBrian

Reputation: 3224

I decided to run g++ 4.8.2 on two test programs, as follows:

fortestfirst.cc:

#include <time.h>
#include <cstdlib>
int main()
{
    srand(time(NULL)); //Prevent optimization of constant size loop
    int loops = rand(), change;
    for (int i = 0; i < loops; i++)
        change = rand();
    for (int i = 0; i < loops; i++)
        change = rand();
}

fortestsecond.cc:

#include <time.h>
#include <cstdlib>
int main()
{
    srand(time(NULL)); //Prevent optimization of constant size loop
    int loops = rand(), change, i;
    for (i = 0; i < loops; i++)
        change = rand();
    for (i = 0; i < loops; i++)
        change = rand();
}

Here is a diff of the two output files from g++ -S FILENAME

1c1
<       .file   "fortestfirst.cc"
---
>       .file   "fortestsecond.cc"
19c19
<       movl    %eax, -12(%rbp)
---
>       movl    %eax, -8(%rbp)
24c24
<       movl    %eax, -16(%rbp)
---
>       movl    %eax, -12(%rbp)
28c28
<       cmpl    -12(%rbp), %eax
---
>       cmpl    -8(%rbp), %eax
30c30
<       movl    $0, -8(%rbp)
---
>       movl    $0, -4(%rbp)
34,35c34,35
<       movl    %eax, -16(%rbp)
<       addl    $1, -8(%rbp)
---
>       movl    %eax, -12(%rbp)
>       addl    $1, -4(%rbp)
37,38c37,38
<       movl    -8(%rbp), %eax
<       cmpl    -12(%rbp), %eax
---
>       movl    -4(%rbp), %eax
>       cmpl    -8(%rbp), %eax

Unfortunately I really do not know much about x86, so I couldn't say exactly what the difference is here (perhaps someone in comments can shed some light), but it does appear that both codes produce the same number of instructions, with the difference being what I'm assuming is an offset.

After following the suggestion from Mats Petersson and compiling both with the -O, -O2, and -O3 flags, both files produced the same assembly code (barring the filename, of course).

Upvotes: 2

dhruv jadia
dhruv jadia

Reputation: 1680

Second way is perfect.Is there problem of memory storage??

Yes.Because in first way initialization of your variable done so if you use that variable during your program or not,memory is already allocated to that variable.

but in second way initialization of variable,memory allocation 'll be done at the time of execution of for loop.

Upvotes: 0

Lawrence H
Lawrence H

Reputation: 467

Performance-wise, it shouldn't matter which you do because your compiler will most likely compile both to the exact same thing anyways. However, I would suggest doing it the second way for readability reasons. It makes it clear to other people that i is used only for iterating through the loop and has no other side effects.

Upvotes: 2

bolov
bolov

Reputation: 75727

Don’t worry about this. Compilers are smart. Very smart. Don’t worry about micro optimization. See my other answer here: https://stackoverflow.com/a/23960961/2805305

The only difference here is that you use 1 variable in the first, 2 variables in the second. But remember how I told you the compilers are smart? They can detect in the second version that the 2 variables never overlap in terms of use, so they can use the same resource for them.

Don’t do micro optimizations!! (*at least until you know what optimizing a C/C++ application actually means, see my linked answer).

Upvotes: 3

j_kubik
j_kubik

Reputation: 6181

Since the first int is never referenced after first loop - as it cannot be, the compiler is free to make above versions exactly the same - and I suspect most sane compilers do.

Anyway, unless you optimize some crutial piece of code or need every byte of stack to avoid possible overflow, you shouldn't even bother. Either way sooner or later somebody will tell you you are doing it wrong.

Upvotes: 2

Pranit Kothari
Pranit Kothari

Reputation: 9841

Second way seems to be more (just little bit) faster because, in first you are first defining, initialising and then using it. While in second one, you are defining,initialising and using same time.

But, as suggested by Namfuak in comments, compiler are smart enough to optimize code, and both can have same impact.

Upvotes: 0

Related Questions