9 Replies - 608 Views - Last Post: 14 July 2013 - 04:38 PM Rate Topic: -----

#1 salazar   User is offline

  • D.I.C Addict

Reputation: 105
  • View blog
  • Posts: 648
  • Joined: 26-June 13

Efficiency of declaring variables in loops

Posted 13 July 2013 - 04:26 PM

Okay, so I have wondered what was better to do inside of loop.

declaring a variable inside a loop each time I need it.
while(true)
{
   int x = someCalculation();
}



or declaring outside the loop.
int x;
while(true)
{
   x = someCalculation();
}


Is This A Good Question/Topic? 0
  • +

Replies To: Efficiency of declaring variables in loops

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3795
  • View blog
  • Posts: 13,738
  • Joined: 08-August 08

Re: Efficiency of declaring variables in loops

Posted 13 July 2013 - 04:55 PM

That looks like the kind of thing that most compilers will handle easily either way. I wouldn't be surprised if the executable was the same for both.
Was This Post Helpful? 0
  • +
  • -

#3 salazar   User is offline

  • D.I.C Addict

Reputation: 105
  • View blog
  • Posts: 648
  • Joined: 26-June 13

Re: Efficiency of declaring variables in loops

Posted 13 July 2013 - 08:36 PM

So the compiler will transform the first to the second automatically? Would the fist not incur more time because of having to allocate new memory?

This post has been edited by salazar: 13 July 2013 - 11:56 PM

Was This Post Helpful? 0
  • +
  • -

#4 Adam27X   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-July 13

Re: Efficiency of declaring variables in loops

Posted 13 July 2013 - 08:52 PM

If you're not doing stuff with x outside the loop I would declare it inside the loop because it makes for clearer code and lets the compiler know its true scope. In terms of efficiency I agree with CT. You might save a nanosecond in compilation time :)
Was This Post Helpful? 0
  • +
  • -

#5 salazar   User is offline

  • D.I.C Addict

Reputation: 105
  • View blog
  • Posts: 648
  • Joined: 26-June 13

Re: Efficiency of declaring variables in loops

Posted 14 July 2013 - 12:01 AM

I don't exactly see how that would make for clear code. If anything all those declarations make for more messy code I think. I'm sorry that I was so vague, I'm not referring to compilation to or just referring to the running time. Does declaring a variable like this without using new or malloc and its relatives allocate memory at all, or does it just let the compiler know I mamy bytes the variable uses?
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is online

  • member icon

Reputation: 5495
  • View blog
  • Posts: 17,070
  • Joined: 25-December 09

Re: Efficiency of declaring variables in loops

Posted 14 July 2013 - 03:30 AM

Quote

I don't exactly see how that would make for clear code. If anything all those declarations make for more messy code I think.

If you are programming in C declaring the variables at the beginning of scopes is considered more acceptable since C90 requires this behavior. However in C++ some people prefer to declare the variables at the time of first use no matter where that is.

Quote

Does declaring a variable like this without using new or malloc and its relatives allocate memory at all

Yes, when you declare a local non-pointer variable you allocate memory, at compile time. The new/malloc functions deal with pointers and they allocate memory at run time.

Jim
Was This Post Helpful? 1
  • +
  • -

#7 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7183
  • View blog
  • Posts: 14,972
  • Joined: 16-October 07

Re: Efficiency of declaring variables in loops

Posted 14 July 2013 - 05:03 AM

As a rule, always give your variable the narrowest scope possible. So, in loop, more better. It should be the same, compiler wise, as noted.

View Postjimblumberg, on 14 July 2013 - 06:30 AM, said:

If you are programming in C declaring the variables at the beginning of scopes is considered more acceptable since C90 requires this behavior.


Note, the OP didn't violate this in their examples.

In C89/C90, variables must be declared at the top of a block, but not just a function block. It's only when you declare them after something else that you fail the standard.
Was This Post Helpful? 1
  • +
  • -

#8 salazar   User is offline

  • D.I.C Addict

Reputation: 105
  • View blog
  • Posts: 648
  • Joined: 26-June 13

Re: Efficiency of declaring variables in loops

Posted 14 July 2013 - 02:47 PM

What do you mean by 'beginning of scopes'? Could you illustrate with an example?

Okay, so i'm getting the feeling that it isn't bad declaring variables in loops. Does it incur any runtime costs? Since, as Jim, points out, that it allocates at compile time, what happens in each iteration. Would it need to reallocate?
Was This Post Helpful? 0
  • +
  • -

#9 Adam27X   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-July 13

Re: Efficiency of declaring variables in loops

Posted 14 July 2013 - 03:38 PM

View Postsalazar, on 14 July 2013 - 02:47 PM, said:

Okay, so i'm getting the feeling that it isn't bad declaring variables in loops. Does it incur any runtime costs? Since, as Jim, points out, that it allocates at compile time, what happens in each iteration. Would it need to reallocate?


It's likely to be in a register either way, depending on what the rest of the code does and how your particular compiler optimizes it. So no, there's no reallocation to be done, because the amount of storage for the variable is known at compile-time and hence the variable is declared on the stack. A pathological case would be if the variable can't fit into a register and the variable spills to memory...but even then it would be in cache if it's used frequently enough.
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg   User is online

  • member icon

Reputation: 5495
  • View blog
  • Posts: 17,070
  • Joined: 25-December 09

Re: Efficiency of declaring variables in loops

Posted 14 July 2013 - 04:38 PM

Quote

What do you mean by 'beginning of scopes'?

Do you understand what scope means in C/C++?


Quote

Okay, so i'm getting the feeling that it isn't bad declaring variables in loops.

No it's not bad. Declaring variables in the scope of a loop keeps the variable local to that loop. When the loop ends the variable is destroyed.

Quote

Does it incur any runtime costs?

It shouldn't incur any additional runtime cost. It should not be reallocated with each loop.

Quote

because the amount of storage for the variable is known at compile-time and hence the variable is declared on the stack.

No the variable is not necessarily declared on the stack. The C standards don't even require a stack to exist, they are only concerned about "automatic" and dynamic. See this link for a very good explanation of this issue.

Also don't confuse yourself with the concept of registers, the standard also doesn't require the use of registers, that's an implementation detail.

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1