Declaring variables in loops

G

Guest

Hi

I usually stick to the convention of not declaring variables in my bodies of
"loops" (including foreach)

ie

int x;

for (int i = 0; i < 10; i++) {
x = ...
}

as opposed to
for (int i = 0; i < 10; i++) {
int x = ...
}

I've heard that in Java declaring variables within loops can cause problems
and I've tried to create some metrics to compare the difference in C# code
between prior to the loop and within the loop.

My results indicated that there was no difference.

I'd like to get a better idea from any one else as to whether my conclusion
is acurate or there is some form of performance hit.

Thanks in advance

AL
 
M

Mike J

below is a smple of how i approach loops..im not a c# guru..but looping to
me crating a new class each time would be a preformance hit
versus create the class once and change its existing value....(no mem clean
up or construction of a new class)

int x=0;
int y=100;
int z=1000;

for (x=0;x<y;x++)
{
z+=x;
Console.WriteLine(z.ToString());
}
Console.ReadKey();

MJ
 
M

Mattias Sjögren

I'd like to get a better idea from any one else as to whether my conclusion
is acurate or there is some form of performance hit.

Performance wise there shouldn't be any difference (as long as you
don't also initialize the variable in the declaration).

Personally I prefer to declare the variable inside the loop, and in
general to keep the variable scope as narrow as possible. That
prevents me from accidentally using a variable after I'm done with it.


Mattias
 
T

Tom Porterfield

AL said:
Hi

I usually stick to the convention of not declaring variables in my bodies of
"loops" (including foreach)

ie

int x;

for (int i = 0; i < 10; i++) {
x = ...
}

as opposed to
for (int i = 0; i < 10; i++) {
int x = ...
}

I've heard that in Java declaring variables within loops can cause problems
and I've tried to create some metrics to compare the difference in C# code
between prior to the loop and within the loop.

My results indicated that there was no difference.

I'd like to get a better idea from any one else as to whether my conclusion
is acurate or there is some form of performance hit.

Thanks in advance

In such a simple scenario as this the IL that's generated is the same so
the performance should also be the same.
 
J

Jon Skeet [C# MVP]

AL said:
I usually stick to the convention of not declaring variables in my bodies of
"loops" (including foreach)

ie

int x;

for (int i = 0; i < 10; i++) {
x = ...
}

as opposed to
for (int i = 0; i < 10; i++) {
int x = ...
}

No, the latter is generally nicer - it avoids "polluting" the namespace
of available variables, and shows that you genuinely intend to only use
the variable within the loop.
I've heard that in Java declaring variables within loops can cause problems

Until I see any evidence of of it, I'd be very sceptical of that.
Certainly in the all the time I've written Java, I've never run into
any issues like that.
and I've tried to create some metrics to compare the difference in C# code
between prior to the loop and within the loop.

My results indicated that there was no difference.

I'd like to get a better idea from any one else as to whether my conclusion
is acurate or there is some form of performance hit.

No, there isn't a performance hit unless you're able to initialize the
variable once and then leave it initialized to the same value. Even
then I'd take the readability improvement of the latter style over the
*possible* slight performance improvement of the former until I'd
proved it was a bottleneck.
 
J

Jon Skeet [C# MVP]

Mike J said:
below is a smple of how i approach loops..im not a c# guru..but looping to
me crating a new class each time would be a preformance hit
versus create the class once and change its existing value....(no mem clean
up or construction of a new class)

int x=0;
int y=100;
int z=1000;

for (x=0;x<y;x++)
{
z+=x;
Console.WriteLine(z.ToString());
}
Console.ReadKey();

Other than z.ToString() which is called on every iteration of the loop,
where do you see objects being created in the above? It's much neater
IMO to do

// These need to be outside the loop anyway
int y = 100;
int z = 1000;

for (int x=0; x < y; x++)
{
z += x;
Console.WriteLine(z.ToString());
}

Bottom line - don't trust instinct about performance without proof,
especially if it affects readability. (In your code it isn't clear that
x is only interesting within the loop - in my code it is.)
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Mike J said:
below is a smple of how i approach loops..im not a c# guru..but looping to
me crating a new class each time would be a preformance hit
versus create the class once and change its existing value....(no mem
clean up or construction of a new class)

I do not understand your example.

Declaring a new instance inside the loop sometime makes sense, some other
times it does not.

if it's a valued type you incurr in no memory overhead at all. (see other;s
post in this thread). With a reference type it might have an impact.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top