469,273 Members | 1,805 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Declaring variables and performance

I have a MSIL/performance question:

Is there any difference between declaring a variable once and assigning
to it multiple times, and declaring and assigning multiple times?

For example:

// Begin sample
for(int i = 0; i < 100; i++)
{
StringBuilder sb = new StringBuilder();
...
}
// End sample

as opposed to

// Begin sample
StringBuilder sb;

for(int i = 0; i < 100; i++)
{
sb = new StringBuilder();
}
// End sample

Are those basically the same, or is there a performance advantage to
the second (or indeed first) method?

Cheers,
Mark

Jun 27 '06 #1
8 5560
Hi Mark,

If you're only using the StrinbBuilder inside the loop, then I'd
declare it inside the loop block so it is scoped to this - which is
good practice. If there is any performance difference it's going to be
very very marginal and totally negligible.

Worrying about this is definitely a case of premature optimisation
(http://www.thejoyofcode.com/Premature_Optimisation.aspx) and possibly
trying too hard too soon
(http://www.thejoyofcode.com/Trying_t...too_soon.aspx)...

:D

Good luck!

Josh

re****@gmail.com wrote:
I have a MSIL/performance question:

Is there any difference between declaring a variable once and assigning
to it multiple times, and declaring and assigning multiple times?

For example:

// Begin sample
for(int i = 0; i < 100; i++)
{
StringBuilder sb = new StringBuilder();
...
}
// End sample

as opposed to

// Begin sample
StringBuilder sb;

for(int i = 0; i < 100; i++)
{
sb = new StringBuilder();
}
// End sample

Are those basically the same, or is there a performance advantage to
the second (or indeed first) method?

Cheers,
Mark


Jun 27 '06 #2
Hi Josh

Thanks. I'm not really trying to optimise the code; just curiosity,
really. I recently got "CLR via C#", and may be suffering from
too-much-information syndrome.

Liked the articles you linked. I've definitely been guilty of premature
optimisation in the past.

Mark

Josh Twist wrote:
Hi Mark,

If you're only using the StrinbBuilder inside the loop, then I'd
declare it inside the loop block so it is scoped to this - which is
good practice. If there is any performance difference it's going to be
very very marginal and totally negligible.

Worrying about this is definitely a case of premature optimisation
(http://www.thejoyofcode.com/Premature_Optimisation.aspx) and possibly
trying too hard too soon
(http://www.thejoyofcode.com/Trying_t...too_soon.aspx)...

:D

Good luck!

Josh


Jun 27 '06 #3
Hi,

They should be the same (performance wise). The only difference is in scope.

Now a better question would had been, what is better , create a new instance
each time or instead use sb.Length = 0 ;

I got a similar situation yesterday but I had no time to decide which is the
"best" way to go ( I used Length=0 )
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

<re****@gmail.com> wrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
I have a MSIL/performance question:

Is there any difference between declaring a variable once and assigning
to it multiple times, and declaring and assigning multiple times?

For example:

// Begin sample
for(int i = 0; i < 100; i++)
{
StringBuilder sb = new StringBuilder();
...
}
// End sample

as opposed to

// Begin sample
StringBuilder sb;

for(int i = 0; i < 100; i++)
{
sb = new StringBuilder();
}
// End sample

Are those basically the same, or is there a performance advantage to
the second (or indeed first) method?

Cheers,
Mark

Jun 27 '06 #4
>I have a MSIL/performance question:

Is there any difference between declaring a variable once and assigning
to it multiple times, and declaring and assigning multiple times?


From an MSIL point of view it doesn't matter. In the compiled code the
variable "declarations" come in the method header, before the IL code.
So where you put your declaration in your C# code doesn't matter.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jun 27 '06 #5
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
They should be the same (performance wise). The only difference is in scope.

Now a better question would had been, what is better , create a new instance
each time or instead use sb.Length = 0 ;

I got a similar situation yesterday but I had no time to decide which is the
"best" way to go ( I used Length=0 )


I'd generally go with creating a new object - that way you're less
likely to end up with it getting into "old" generations which will then
be garbage collected later on.

Note that in C# 2.0, the difference in scope can affect behaviour if
the variable is used in an anonymous method.

--
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
Jun 27 '06 #6
As I suspected, I just tried a quick experiment and both compile to
exactly the same MSIL (the compiler recognises that the var isn't used
outside the block so moves its declaration inside).

I guess there is another factor to reinforce our decision to place it
inside the block though - readability. If I see a variable declared
inside a block I know it isn't used in anyway outside of that block.

Josh
http://www.thejoyofcode.com/

Jon wrote:
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
They should be the same (performance wise). The only difference is in scope.

Now a better question would had been, what is better , create a new instance
each time or instead use sb.Length = 0 ;

I got a similar situation yesterday but I had no time to decide which is the
"best" way to go ( I used Length=0 )


I'd generally go with creating a new object - that way you're less
likely to end up with it getting into "old" generations which will then
be garbage collected later on.

Note that in C# 2.0, the difference in scope can affect behaviour if
the variable is used in an anonymous method.

--
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


Jun 28 '06 #7
Hi,
Good point.

One possible good side effect of reusing the previous one is that the new
string most likely is of similar size, so if the StringBuilder had to
increase the size it will not have to do that again.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
They should be the same (performance wise). The only difference is in
scope.

Now a better question would had been, what is better , create a new
instance
each time or instead use sb.Length = 0 ;

I got a similar situation yesterday but I had no time to decide which is
the
"best" way to go ( I used Length=0 )


I'd generally go with creating a new object - that way you're less
likely to end up with it getting into "old" generations which will then
be garbage collected later on.

Note that in C# 2.0, the difference in scope can affect behaviour if
the variable is used in an anonymous method.

--
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

Jun 28 '06 #8
Thanks to everyone for the replies and info. I guess I really should
start looking at the MSIL myself.

Mark

Jun 30 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Oliver Corona | last post: by
1 post views Thread by ColinWard | last post: by
115 posts views Thread by Mark Shelor | last post: by
6 posts views Thread by Mark A. Sam | last post: by
6 posts views Thread by =?Utf-8?B?QUw=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.