469,347 Members | 3,452 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,347 developers. It's quick & easy.

Declaring variables in loops

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

May 23 '07 #1
6 2728
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

"AL" <AL@discussions.microsoft.comwrote in message
news:38**********************************@microsof t.com...
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

May 23 '07 #2
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 23 '07 #3
AL wrote:
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.
--
Tom Porterfield
May 23 '07 #4
AL <AL@discussions.microsoft.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 23 '07 #5
Mike J <ve***********@sbcglobal.netwrote:
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 23 '07 #6
Hi,

"Mike J" <ve***********@sbcglobal.netwrote in message
news:vC****************@newssvr19.news.prodigy.net ...
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.
May 23 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by sarmin kho | last post: by
3 posts views Thread by Grey Plastic | last post: by
28 posts views Thread by Alf P. Steinbach | last post: by
1 post views Thread by ColinWard | last post: by
9 posts views Thread by Javaman59 | last post: by
6 posts views Thread by Mark A. Sam | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.