【问题标题】:performance consideration when using properties multiple times多次使用属性时的性能考虑
【发布时间】:2011-01-14 11:02:53
【问题描述】:

我在使用string.format 格式化我的字符串时使用CultureInfo.CurrentCulture

引用this blog

这只是暗示如果 你经常使用 CurrentCulture,它 可能值得一读 私有变量而不是制作 很多电话 CultureInfo.CurrentCulture,否则 你用完了时钟周期 不必要的。

按照作者的说法

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

优于

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 a property

多次访问一个属性时是否存在相关的性能损失??

我还做了一些经验分析,我的测试表明使用局部变量比直接使用属性更昂贵。

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);

结果:

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

我的测试表明,使用CultureInfo.CurrentCulture 属性比使用局部变量更好(这与作者的观点相矛盾)。还是我在这里遗漏了什么?

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

【问题讨论】:

  • 在您的测试代码中,您不会重置秒表。使用缓存的引用实际上更快。
  • CultureInfo.CurrentCulture 并不便宜,但 string.Format 更昂贵。
  • 史蒂文,你说得对,我没有重置秒表。您为什么不将其发布为答案,我将更新我的帖子+将您的帖子标记为答案。对于那些好奇的人,是的,使用局部变量会更快。 1亿次迭代相差约2.473秒!!!
  • +1 这是你的一个非常有趣的问题,Ram。为了支持你的观点,我在 SO 上的某处读到访问属性比私有不可变成员更昂贵。

标签: c# performance optimization properties


【解决方案1】:

只有在分析器显示 CultureInfo.CurrentCulture 在您的代码中是一个重大问题并且放入局部变量会使其更快时,您才应该将其优化为局部变量。此配置文件显示两者都不正确,因此我不会将其放入本地。

【讨论】:

    【解决方案2】:

    重写代码的一个真正原因

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

    来自

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

    是为了可读性和可维护性。现在,如果您出于某种原因需要将文化从CultureInfo.CurrentCulture 更改为通过某个配置文件加载或作为方法传入参数的CultureInfo,您只需在一个地方更改代码。 性能在这里是次要考虑因素,可能无关紧要,因为这极不可能成为代码中的瓶颈。

    【讨论】:

      【解决方案3】:

      您的代码中存在错误。在您的测试代码中,您不会重置秒表。重置秒表时,您会发现使用缓存的引用实际上更快。 CultureInfo.CurrentCulture 并不便宜,但 string.Format 的成本要高得多。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-25
        • 2011-08-05
        相关资源
        最近更新 更多