如何编译自动属性? [英] How an auto property is compiled?

查看:29
本文介绍了如何编译自动属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是在看到一些关于属性和自动属性以及它们如何由编译器表示的问题和答案之后提出这个问题的.

I am asking this after I have seen some questions and answers about properties and auto properties and how these are represented by the compiler.

据我所知,自动属性表示为一个字段,有两种方法,一个 getter 和一个 setter.在这种情况下,如果使用一个字段,访问该字段的代码应该比访问属性的代码更快,因为它避免了对方法的任何补充调用.为了证明这个理论,我编写了以下代码,请原谅它的外观:

From what I understood, an auto-property is represented as a field, with two methods, a getter an a setter. In this case if a field is used, the code to access that field should be faster than the code which accesses a property, because it avoids any suplimentar call to methods. In order to prove this theory I was writing the following code, and please, excuse me for how it looks:

public class A
{
    public int Prop { get; set; }
    public int Field;

    public A()
    {
        Prop = 1;
        Field = 1;
    }
}
class Program
{
    static void Main(string[] args)
    {
        List<long> propertyExecutionTimes = new List<long>();
        List<long> fieldExecutionTimes = new List<long>();

        A a = new A();
        int aux;
        for (int j = 0; j < 100; j++)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();
            for (int i = 0; i < 10000000; i++)
            {
                aux = a.Prop;
                a.Prop = aux;
            }
            watch.Stop();

            propertyExecutionTimes.Add(watch.ElapsedMilliseconds);

            watch = System.Diagnostics.Stopwatch.StartNew();
            for (int i = 0; i < 10000000; i++)
            {
                aux = a.Field;
                a.Field = aux;
            }
            watch.Stop();

            fieldExecutionTimes.Add(watch.ElapsedMilliseconds);
        }
        Console.WriteLine("Property best time: " + propertyExecutionTimes.OrderBy(x => x).First());
        Console.WriteLine("Field best time: " + fieldExecutionTimes.OrderBy(x => x).First());

        Console.ReadKey();
    }
}

它包括对一个字段调用 10M 次,对比对属性调用 10M 次 10 次,然后从每个测试中选择最低值.VS2012 发布版本的结果是:

It consists of calling 10M times a field, vs calling 10M times a property everthing 10 times, and then it is picked the lowest value from each test. The results with a VS2012 release version were:

Property best time: 96
Field best time: 45

因此,正如预期的那样,字段的结果比属性要好得多.

So as expected the results for a field were quite better than a property.

但是在此之后,我在没有调试器的情况下运行了程序(只需运行 .exe 文件),令我惊讶的是,使用字段和这样的属性的时间相似:

But after this I runned the program without debugger (simply run the .exe file) and as a surprise for me, the times were similar for both using field and a property like this:

Property best time: 20
Field best time: 20

在这种情况下,使用属性和使用字段之间的区别是没有的,这让我认为它们在这种情况下被编译为相同的,或者属性的方法被转换为类似于 inline C++ 中的方法.这让我觉得我应该看看实际的 getter 和 setter 是如何与属性相关联的,所以我添加了一对 getter 和 setter:

In this case the differences between using a property and a field were none, which makes me consider that they are compiled in this case as the same, or the methods of the property were converted to something similar with inline methods in C++. This made me think I should see how actual getters and setters are working copared to the property, so I have added a pair of getters and setters:

private int _field;
public int Get() { return _field; }
public void Set(int value) { _field = value; }

针对属性进行类似测试,并进行一些更改以尊重相同的行为:

Similar testing this against a property with a few changes to respect the same behavior:

aux = a.Get();
a.Set(aux);

给了我这个输出:

Property best time: 96
Methods best time: 96

使用调试器和:

Property best time: 20
Methods best time: 20

没有调试器.这些值是相同的,所以我得出结论,自动属性是像字段一样编译的 getter 和 setter.这是一个正确的结论吗?最后,为什么在附加调试器时字段也比属性更快?

Without debugger. These values are the same, so I concluded that auto-properties are getters and setters which are compiled just like fields. Is this a correct conclusion? And finaly, why the field was also faster than a property when the debugger was attached?

推荐答案

这些值是相同的,所以我得出结论,自动属性是 getter 和 setter,它们就像字段一样编译.

These values are the same, so I concluded that auto-properties are getters and setters which are compiled just like fields.

是的,自动属性实现为一个字段,以及该字段的 get 和可选的 set 访问器.

Yes, auto-properties are implemented as a field, and a get and optionally a set accessor for that field.

这是一个正确的结论吗?

Is this a correct conclusion?

出于错误的原因,但是是的.:) 要准确地了解自动属性是如何实现的,不要依赖时间,而是创建一个程序或库并在 MSIL 反汇编程序中打开它.如您所见,计时结果可能会产生误导.

For the wrong reasons, but yes. :) To accurately see how auto-properties are implemented, don't rely on timing, but create a program or library and open that in a MSIL disassembler. As you've seen, timing results can be misleading.

最后,为什么在附加调试器时字段也比属性更快?

And finaly, why the field was also faster than a property when the debugger was attached?

在调试时,内联之类的机会要少得多,因为内联使设置断点、支持编辑和继续等变得更加困难.主要是内联使属性访问器与直接字段一样快访问.

When debugging, there are far fewer opportunities for things like inlining, since inlining makes it more difficult to set a break point, to support edit-and-continue, etc. It's mainly inlining that makes property accessors as fast as direct field access.

这篇关于如何编译自动属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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