c#dot operator overhead:效率更高

时间:2017-03-31 20:59:44

标签: c# dot-operator

所以我有一个WPF应用程序,它有一个带有子MVVM的基本MVVM。我试过谷歌搜索答案,但不确定技术术语,所以我将在下面提供两个例子,也许有人可以给我一些关于例子效率的见解。我想知道开销或重要性是否存在微小差异。

假设我的设置类似于以下

public class ParentViewModel
{
    public ParentViewModel()
    {
        Child = new ChildViewModel();
    }

    public ChildViewModel Child { get; set; }
}

public class ChildViewModel
{
    public ChildViewModel()
    {
        GrandChild = new GrandChildViewModel();
    }

    public GrandChildViewModel GrandChild { get; set; }
}

public class GrandChildViewModel
{
    public GrandChildViewModel()
    {
        GreatGrandChild = new GreatGrandChildViewModel();
    }

    public GreatGrandChildViewModel GreatGrandChild { get; set; }
}

public class GreatGrandChildViewModel
{
    public GreatGrandChildViewModel()
    {
         intA = 1;
         intB = 2;
         intC = 3;
    }

    public int intA { get; set; }
    public int intB { get; set; }
    public int intC { get; set; }
}

以下两个使用示例是我想要洞察的地方。

示例1:

public Main()
{
     var parent = new ParentViewModel();

     Console.WriteLine($"A: {parent.Child.GrandChild.GreatGrandChild.intA}" +
                       $"B: {parent.Child.GrandChild.GreatGrandChild.intB}" +
                       $"C: {parent.Child.GrandChild.GreatGrandChild.intC}");
}

示例2:

public Main()
{
     var greatGrandChild = new ParentViewModel().Child.GrandChild.GreatGrandChild;

     Console.WriteLine($"A: {greatGrandChild.intA}" +
                       $"B: {greatGrandChild.intB}" +
                       $"C: {greatGrandChild.intC}");
}

哪一个效率更高?我问,因为我认为示例2会更有效率,因为它会降低到最低级别一次,然后访问intA,intB和intC。 这有关系吗?性能差异是否显着?

3 个答案:

答案 0 :(得分:1)

你会发现两者之间绝对没有优化。实际上,我怀疑编译器会将两种类型的语句优化到同一个IL中。

然而,后一个例子更具可读性,所以我选择这种方法。

答案 1 :(得分:1)

我建议您获取所需的最小对象。

在您给出的示例中,性能差异可以忽略不计,但如果父/祖父/宏观对象中有更多数据,并且您传递此对象(特别是通过网络),则它可以有所作为。想象一下,将某人的整个家族树对象传递给一些真正只需要该人姓名的Web服务。

但它也通过抓住你需要的最小物体来显示你的意图。有意编程通常更容易阅读和维护,并允许您更容易地发现错误。

答案 2 :(得分:1)

虽然最初的想法是编译器会优化到相同的IL,但显然不是真的

虽然我没有检查过IL,但快速而肮脏的秒表测试表明孙子路线要快得多

使用问题中的视图模型,结果如下:

var parent = new ParentViewModel();
var greatGrandChild = new ParentViewModel().Child.GrandChild.GreatGrandChild;
var watch = new Stopwatch();

var a = parent.Child.GrandChild.GreatGrandChild.intA;
var b = parent.Child.GrandChild.GreatGrandChild.intB;
var c = parent.Child.GrandChild.GreatGrandChild.intC;

var bothTotal = 0L;
var longTotal = 0L;
var shortTotal = 0L;

watch.Start();
a = parent.Child.GrandChild.GreatGrandChild.intA;
b = parent.Child.GrandChild.GreatGrandChild.intB;
c = parent.Child.GrandChild.GreatGrandChild.intC;
a = greatGrandChild.intA;
b = greatGrandChild.intB;
c = greatGrandChild.intC;
watch.Stop();
bothTotal += watch.ElapsedTicks;
watch.Reset();          
Console.WriteLine("Longhand and Shorthand: " + bothTotal);

watch.Start();
a = parent.Child.GrandChild.GreatGrandChild.intA;
b = parent.Child.GrandChild.GreatGrandChild.intB;
c = parent.Child.GrandChild.GreatGrandChild.intC;
a = parent.Child.GrandChild.GreatGrandChild.intA;
b = parent.Child.GrandChild.GreatGrandChild.intB;
c = parent.Child.GrandChild.GreatGrandChild.intC;
watch.Stop();
longTotal += watch.ElapsedTicks;
watch.Reset();          
Console.WriteLine("Longhand Only: " + longTotal);

watch.Start();
a = greatGrandChild.intA;
b = greatGrandChild.intB;
c = greatGrandChild.intC;
a = greatGrandChild.intA;
b = greatGrandChild.intB;
c = greatGrandChild.intC;
watch.Stop();
shortTotal += watch.ElapsedTicks;
watch.Reset();          
Console.WriteLine("Shorthand Only: " + shortTotal);

这些是我的典型结果:

Longhand and Shorthand: 22
Longhand Only: 3
Shorthand Only: 2

这显然不是最细微的测试,并且肯定有关于它的事情可以被认为倾向于确认偏差 - 但从表面上看,它确实表现为'简写'路线更优化,值得进一步测试

<强>更新

我经历了检查生成的IL,结果很清楚:

普通写法:

// Setup the object:
newobj     instance void ParentViewModel::.ctor()

// Actually call the members:
callvirt   instance class ChildViewModel ParentViewModel::get_Child()
callvirt   instance class GrandChildViewModel ChildViewModel::get_GrandChild()
callvirt   instance class GreatGrandChildViewModel 
GrandChildViewModel::get_GreatGrandChild()
callvirt   instance int32 GreatGrandChildViewModel::get_intA()
callvirt   instance class ChildViewModel ParentViewModel::get_Child()
callvirt   instance class GrandChildViewModel ChildViewModel::get_GrandChild()
callvirt   instance class GreatGrandChildViewModel 
GrandChildViewModel::get_GreatGrandChild()
callvirt   instance int32 GreatGrandChildViewModel::get_intB()
callvirt   instance class ChildViewModel ParentViewModel::get_Child()
callvirt   instance class GrandChildViewModel ChildViewModel::get_GrandChild()
callvirt   instance class GreatGrandChildViewModel 
GrandChildViewModel::get_GreatGrandChild()
callvirt   instance int32 GreatGrandChildViewModel::get_intC()

简写:

// Setup the object
newobj     instance void ParentViewModel::.ctor()
call       instance class ChildViewModel ParentViewModel::get_Child()
callvirt   instance class GrandChildViewModel ChildViewModel::get_GrandChild()
callvirt   instance class GreatGrandChildViewModel GrandChildViewModel::get_GreatGrandChild()

// Actually call the members:
callvirt   instance int32 GreatGrandChildViewModel::get_intA()
callvirt   instance int32 GreatGrandChildViewModel::get_intB()
callvirt   instance int32 GreatGrandChildViewModel::get_intC()