性能开销在.NET属性 [英] Performance overhead for properties in .NET

查看:123
本文介绍了性能开销在.NET属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读的地方,具有公共属性,是preferable具有公共成员中的一类。

  1. 这是唯一abstaraction和模块化的原因?是否有任何其他压倒一切的原因是什么?

  2. 属性访问都conerted到编译器的函数调用。对于没有备份存储特性(如公共字符串用户名{获取;集;} ),这将是比较直接的成员访问的性能开销? (我知道这不是通常应该有所作为,但在我的一些code,属性访问数百万次。)

EDIT1: 我跑了一些测试code在整数成员和属性和公众成员约3-4倍的速度性能。 (〜57毫秒。VS〜206毫秒,在调试和57与97的版本是最常见的运行值)。 1000万的读取和写入,两者都足够小,没有理由改变任何东西。

code:

 类TestTime1
{
    公共TestTime1(){}
    公众诠释ID = 0;
}
类TestTime2
{
    公共TestTime2(){}
    [默认值(0)]
    公众诠释ID {获得;组; }
}


类节目
{
    静态无效的主要(字串[] args)
    {
        尝试
        {
            TestTime1时间1 =新TestTime1();
            TestTime2时间2 =新TestTime2();
            秒表监视1 =新的秒表();
            秒表watch2 =新的秒表();
            watch2.Start();
            的for(int i = 0; I<千万;我++)
            {
                time2.ID =我;
                I = time2.ID;
            }
            watch2.Stop();
            watch1.Start();
            的for(int i = 0; I<千万;我++)
            {
                time1.id =我;
                I = time1.id;
            }
            watch1.Stop();
            Console.WriteLine(时间为1和2:{0},{1},watch1.ElapsedMilliseconds,watch2.ElapsedMilliseconds);

        }
        赶上(例外前)
        {
            Console.WriteLine(ex.Message);
        }
        Console.In.ReadLine();
    }
}
 

解决方案
  

只有abstaraction和模块化的,因为这个?是否有任何其他压倒一切的原因是什么?

不,我知道的;这些理由本身吸引力不够。但也许别人会跳这个。

  

属性访问都conerted到编译器的函数调用。对于没有备份存储特性(如公共用户名字符串{获取;集;}),这将是比较直接的成员访问的性能开销? (我知道这不是通常应该有所作为,但在我的一些code,属性访问数百万次。)

在所得中间语言,属性访问被转换为一个方法调用。然而,随着话说,这只是一种中间语言:它被编译刚刚在时间下到别的东西。这种转换步骤还包括优化喜欢琐碎的方法,如简单的属性访问器内联。

我希望(但你需要测试以确保)的抖动采取此类访问的照顾,所以应该没有性能上的差异。

I read somewhere that having public properties is preferable to having public members in a class.

  1. Is this only because of abstaraction and modularity? Are there any other over-riding reasons?

  2. The property accesses are conerted into function calls by the compiler. For properties without a backup store (e.g. public string UserName { get; set; }), what would be the performance overhead compared to a direct member access? (I know it shouldn't usually make a difference but in some of my code, properties are accessed millions of times.)

Edit1: I ran some test code over integer members and Properties and the public members were about 3-4 times as fast as Properties. (~57 ms. vs ~206 ms. in Debug and 57 vs. 97 in Release was the most common run value). For 10 million reads and writes, both are small enough not to justify changing anything.

Code:

    class TestTime1
{
    public TestTime1() { }
    public int id=0;
}
class TestTime2
{
    public TestTime2() { }
    [DefaultValue(0)]
    public int ID { get; set; }
}


class Program
{
    static void Main(string[] args)
    {
        try
        {
            TestTime1 time1 = new TestTime1();
            TestTime2 time2 = new TestTime2();
            Stopwatch watch1 = new Stopwatch();
            Stopwatch watch2 = new Stopwatch();
            watch2.Start();
            for (int i = 0; i < 10000000; i++)
            {
                time2.ID = i;
                i = time2.ID;
            }
            watch2.Stop();
            watch1.Start();
            for (int i = 0; i < 10000000; i++)
            {
                time1.id = i;
                i = time1.id;
            }
            watch1.Stop();
            Console.WriteLine("Time for 1 and 2 : {0},{1}",watch1.ElapsedMilliseconds,watch2.ElapsedMilliseconds);

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Console.In.ReadLine();
    }
}

解决方案

Is this only because of abstaraction and modularity? Are there any other over-riding reasons?

Not that I know of; these reasons are by themselves compelling enough. But maybe someone else will jump in on this.

The property accesses are conerted into function calls by the compiler. For properties without a backup store (e.g. public string UserName { get; set; }), what would be the performance overhead compared to a direct member access? (I know it shouldn't usually make a difference but in some of my code, properties are accessed millions of times.)

In the resulting Intermediate Language, a property access is translated to a method call. However, as the word says, this is only an Intermediate Language: it gets compiled Just-In-Time down to something else. This translation step also involves optimizations like inlining of trivial methods, such as simple property accessors.

I would expect (but you'd need to test to make sure) that the JITter takes care of such accessors, so there should be no performance difference.

这篇关于性能开销在.NET属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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