Declaring variables and performance

  • Thread starter Thread starter rendle
  • Start date Start date
R

rendle

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
 
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_too_hard_too_soon.aspx)...

:D

Good luck!

Josh
 
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
 
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 )
 
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
 
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us> said:
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.
 
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/
 
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
 
Thanks to everyone for the replies and info. I guess I really should
start looking at the MSIL myself.

Mark
 
Back
Top