.NET Framework Bookmark and Share   
 index > Common Language Runtime > GC and primitive types initialization cost
 

GC and primitive types initialization cost

Hi, I have this trivial issue.
<pre lang="x-c#">//implementation no. 1
for (int i = 0; i < 10; i++)
{
int a = i;
}

//implementation no. 2
int a
for (int i = 0; i < 10; i++)
{
a = i;
}
What is better in terms of performance ? Because the primitive types are not true value types plus there is a generational GC, which could make theoreticallythe first case faster. Butmy intuition says that the second's faster.It has been on my mind for some time.
If you know some articles on this topic, please share them with me.
Thx for help.
W8eR_CZech
The GC is not involved in either operation, since in this case, you are using a value type (System.Int32). This means that "a" is allocated on the stack, not on the heap, and is not tracked by the GC.

In general, the first case will most likely have a (very, very slight, but probably not even easily measurable) performance improvement, since it's doing fewer stack allocations.


If, however, you're doing things with reference types (ie: MyClass a = new MyClass();), things change. It is actually VERY difficult to know, just by the code, which will be faster. In many cases, the first option is actually faster because it prevents the objects from going into Gen 2 in the GC, even though it's technically more allocations.

However, I recommend against trying to optimize code at this level, unless you find a true, profiled (measured) performance issue. You will find that you have much better results if you try to optimize your algorithm rather than your code, especially at this level.
Reed Copsey, Jr. - http://reedcopsey.com
  • Marked As Answer byW8eR_CZech Sunday, September 20, 2009 7:05 PM
  •  
Reed Copsey, Jr.
It makes no difference at all. You are not using any objects that are garbage collected. The "a" variable is just a stack location, it stores a value type. The only difference is scope, that's a compiler detail.

Measure this before you post, use the System.Diagnostics.Stopwatch class.

Hans Passant.
  • Unmarked As Answer byW8eR_CZech Sunday, September 20, 2009 7:05 PM
  • Marked As Answer byW8eR_CZech Sunday, September 20, 2009 7:03 PM
  •  
nobugz
The GC is not involved in either operation, since in this case, you are using a value type (System.Int32). This means that "a" is allocated on the stack, not on the heap, and is not tracked by the GC.

In general, the first case will most likely have a (very, very slight, but probably not even easily measurable) performance improvement, since it's doing fewer stack allocations.


If, however, you're doing things with reference types (ie: MyClass a = new MyClass();), things change. It is actually VERY difficult to know, just by the code, which will be faster. In many cases, the first option is actually faster because it prevents the objects from going into Gen 2 in the GC, even though it's technically more allocations.

However, I recommend against trying to optimize code at this level, unless you find a true, profiled (measured) performance issue. You will find that you have much better results if you try to optimize your algorithm rather than your code, especially at this level.
Reed Copsey, Jr. - http://reedcopsey.com
  • Marked As Answer byW8eR_CZech Sunday, September 20, 2009 7:05 PM
  •  
Reed Copsey, Jr.

You can use google to search for other answers

Custom Search

More Threads

• How can I communicate with managed exe and unManged exe (C# and C++)
• Disassembling code (ILDASM)
• Marshalling COM interface between threads--help!
• Bad Data problem...
• Performance counters enumeration is shown not just in english
• Invoke Interactive Windows Logon from Manged Service (Vista, XP and 2000)
• I am trying to retrieve a selected value from a listbox and store it in a string variable.
• memory management and memory leaks
• how to specify .net runtime for winforms in HTML pages
• Application Security Setting