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 8 5825
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
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
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
>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.
<"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
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
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
Thanks to everyone for the replies and info. I guess I really should
start looking at the MSIL myself.
Mark This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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. ...
|
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...
|
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()...
|
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,...
|
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++) {
|
by: nguillot |
last post by:
Hello.
If I have the following classes:
class B {};
typedef B tB;
if A is:
class A
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
| |