473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 5824
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 allocating memory for a new variable more...
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 variable set to the name of the control to tell the...
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 a static array within a performance-critical...
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 use DIM for declaring the variables or Static. ...
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. For example Public Test1 as String I'll get en...
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 WebForm2.aspx. For declaring the property oRpte()...
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 and initializing variables inside a loop routine,...
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 < 10; i++) {
8
by: nguillot | last post by:
Hello. If I have the following classes: class B {}; typedef B tB; if A is: class A
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.