472,353 Members | 1,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 5721
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Oliver Corona | last post by:
I am wondering if anyone has any insights on the performance benefit (or detriment) of declaring local variables instead of referencing members. Is...
1
by: ColinWard | last post by:
Hi guys. I have a question about declaring variables. I do a lot of re-querying of controls in my database and I use the Set statement with a...
115
by: Mark Shelor | last post by:
I've encountered a troublesome inconsistency in the C-language Perl extension I've written for CPAN (Digest::SHA). The problem involves the use of...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either...
6
by: Mark A. Sam | last post by:
Hello, I am using Visual Web Developer 2005 Express. I want to declare a varible, using Visual Basic as the language and can't get anywhere. ...
3
by: jbeteta | last post by:
Hello, I have a problem declaring variables. I need to create an object oRpte as ReportClass on WebForm1.aspx and be able to use its value on...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring...
6
by: =?Utf-8?B?QUw=?= | last post by:
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...
8
by: nguillot | last post by:
Hello. If I have the following classes: class B {}; typedef B tB; if A is: class A
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.