使用属性时的性能考虑多次 [英] performance consideration when using properties multiple times

查看:139
本文介绍了使用属性时的性能考虑多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用使用的String.Format

要报价这个博客

这只是有,如果
你正在使用的CurrentCulture了很多的寓意,它
可能是值得一读入一个
私有变量,而不是让
批次调用
CultureInfo.CurrentCulture,否则
你使用了时钟周期
不必要的。

This just has the implication that if you are using CurrentCulture a lot, it might be worth reading it into a private variable rather than making lots of calls to CultureInfo.CurrentCulture, otherwise you're using up clock cycles needlessly.

因此按照这个作者

var culture = CultureInfo.CurrentCulture
string.Format(culture,"{0} some format string","some args");
string.Format(culture,"{0} some format string","some other args");



优于

is better than

string.Format(CultureInfo.CurrentCulture,"{0} some format string","some args");
string.Format(CultureInfo.CurrentCulture,"{0} some format string","some other args");



根据MSDN,的 CultureInfo.CurrentCulture是

属性是有性能损失有关访问属性多次??

Is there a performance penalty associated when accessing a property multiple times ??

当我也做了一些emperical分析,我的测试告诉我,使用一个局部变量是不是直接使用属性更加昂贵。

Also I did some emperical analysis and my tests show me that using a local variable is more expensive than using the property directly.

Stopwatch watch = new Stopwatch();

            int count = 100000000;
            watch.Start();
            for(int i=0;i<count;i++)
            {
                string.Format(CultureInfo.CurrentCulture, "{0} is my name", "ram");
            }


            watch.Stop();
                 //EDIT:Reset watch
                 watch.Reset();


            Console.WriteLine(watch.Elapsed);
            Console.WriteLine(watch.ElapsedMilliseconds);
            Console.WriteLine(watch.ElapsedTicks);


            Console.WriteLine("--------------------");
            var culture = CultureInfo.CurrentCulture;
            watch.Start();
            for (int i=0; i < count; i++)
            {
                string.Format(culture, "{0} is my name", "ram");
            }


            watch.Stop();

            Console.WriteLine(watch.Elapsed);
            Console.WriteLine(watch.ElapsedMilliseconds);
            Console.WriteLine(watch.ElapsedTicks);



结果:

Result:

00:00:29.6116306
29611
68922550970
--------------------
00:00:27.3578116
27357
63676674390

我的测试表明,使用 CultureInfo.CurrentCulture 性能比使用本地变量(与作者查看矛盾)更好。还是我失去了一些东西在这里。

My tests show that using CultureInfo.CurrentCulture property is better than using local variable (which contradicts with the authors view). Or am I missing something here ?

编辑:我德第二次迭代之前没有重置秒表。因此差。重置秒表,更新迭代次数,并导致该修改

I was not resetting the stopwatch before teh second iteration. hence the difference. resetting stopwatch, updating iteration count and result in this edit

推荐答案

有在你的代码中的bug。在你的测试代码,你不要重置秒表。当您重置驻足观看,你会发现使用缓存引用实际上要快。 CultureInfo.CurrentCulture是不便宜,但的String.format是更昂贵的。

There is a bug in your code. In your test code you don't reset the Stopwatch. When you reset the stop watch, you'll see that using the cached reference is actually faster. CultureInfo.CurrentCulture isn't cheap, but string.Format is much more costlier.

这篇关于使用属性时的性能考虑多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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