Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Good day,

Please, indicate: can local reference variables increase performance of .NET applications? In particular, is Method2, indicated below, faster than Method1? What are drawbacks of Method2 regarding memory usage and the GarbageCollector?


public void Method1()
{
    for (int i = 0; i < Count; i++)
    {
        OuterObject.InnerList[i].X = 0.0;
        OuterObject.InnerList[i].Y = 5.0 + i;
        OuterObject.InnerList[i].Z = Math.Sin(i);
        //...
    }
}

public void Method2()
{
    for (int i = 0; i < Count; i++)
    {
        // Assign a local variable, which is a reference
        // on the item class, having X,Y,Z double properties.
        var item = OuterObject.InnerList[i];

        item.X = 0.0;
        item.Y = 5.0 + i;
        item.Z = Math.Sin(i);
        //...
    }
}
Posted
Updated 29-Jul-15 2:15am
v2
Comments
Kornfeld Eliyahu Peter 29-Jul-15 8:24am    
Why not to compile and check the IL result to see what are the real differences?

Indeed you should check the IL to see if there are any differences. My guess would be there aren't because the temp variable item would in compiled code be a processor register. If the compiler is any good it will recognize the OuterObject.InnerList[i] repetition and won't reload it all starting at OuterObject but reuse the reference it already got. hence, no difference at all. You might see some difference if you compile in debug mode because no optimization is done then. In release mode the compiler does optimize and won't need to represent the original sourcecode completely. So you might want to check out both to get the best grade of your class because I think not all your other classmates might think of that ;-)

Good luck!
 
Share this answer
 
Comments
SuperSlavik 29-Jul-15 12:01pm    
Thank you for your answer!
E.F. Nijboer 29-Jul-15 13:27pm    
no problem :-)
That's actually a difficult question to answer, because it's not as simple as "do this, it'll be quicker". And the reason why is called "optimization". Just as you "optimize out" the dereference and array index access into a local variable in creating the second version, so can the compiler. But...it doesn't do that for debug builds by default - and it does do that for release builds by default!

So the answer is "maybe" in practice.

To really assess it you need to either look at eth debug and release IL, or time both loops using the Stopwatch class for maybe a million iterations - again, both in debug and release version.

Does either use more memory? Well, probably no. The IL the compiler generates will probably consume the same stack space, and the heap space won't be affected anyway. The final native code probably will use registers instead of stack memory anyway, so there won't be any real difference there either!

And none of this affects the Garbage Collector, because nothing here allocates new memory, or removes references to objects.
 
Share this answer
 
Comments
SuperSlavik 29-Jul-15 12:01pm    
Thank you for your answer!
OriginalGriff 29-Jul-15 12:11pm    
You're welcome!
Yes because your methods are non-static
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900