本地引用变量可以提高.NET应用程序的性能吗? [英] Can local reference variables improve performance in .NET applications?

查看:123
本文介绍了本地引用变量可以提高.NET应用程序的性能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,



请指出:本地引用变量可以提高.NET应用程序的性能吗?特别是,Method2,如下所示,比Method1更快? Method2关于内存使用和GarbageCollector的缺点是什么?





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);
        //...
    }
}

推荐答案

确实你应该检查IL到看看是否有任何差异。我的猜测是没有,因为temp变量 item 在编译的代码中会是一个处理器寄存器。如果编译器有任何好处,它将识别 OuterObject.InnerList [i] 重复,并且不会从 OuterObject 但重用它已经获得的引用。因此,没有任何区别。如果在调试模式下编译,可能会看到一些差异,因为那时没有进行优化。在发布模式下,编译器会进行优化,而不需要完全代表原始源代码。所以你可能想看看两者都能得到你班上最好的成绩,因为我觉得不是所有其他同学都会想到这一点;-)



祝你好运!
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!


这实际上是一个难以回答的问题,因为它并不像做这个,它会更快那么简单。其原因被称为优化。正如您在创建第二个版本时解析取消引用和数组索引访问本地变量一样,编译器也是如此。但是......默认情况下它不会对调试版本执行此操作 - 默认情况下它会对发布版本执行此操作!



所以答案是可能在实践中。



要真正评估它,您需要查看eth调试并释放IL,或者使用Stopwatch类进行两次循环计时可能需要一百万次迭代 - 再次,调试和发布版本。



是否使用更多内存?好吧,可能没有。编译器生成的IL可能会消耗相同的堆栈空间,并且堆空间无论如何都不会受到影响。最终的本机代码可能会使用寄存器而不是堆栈内存,因此也不会有任何真正的区别!



这些都不会影响垃圾收集器,因为这里没有任何东西分配新的内存,或者删除对象的引用。
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.


是的,因为你的方法是非静态的
Yes because your methods are non-static


这篇关于本地引用变量可以提高.NET应用程序的性能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆