接口与铸造类铸件 [英] Interface Casting vs. Class Casting
问题描述
我一直认为,导致铸件可以,在某些情况下,成为对性能产生较大的障碍。这可能是moreso当我们开始讨厌的异常抛出语无伦次网处理\追赶的情况。
I've been led to believe that casting can, in certain circumstances, become a measurable hindrance on performance. This may be moreso the case when we start dealing with incoherent webs of nasty exception throwing\catching.
由于我要创造更多的正确的试探,当涉及到编程,我已经提示问这个问题到.NET大师在那里?难道界面比铸造压铸类快
Given that I wish to create more correct heuristics when it comes to programming, I've been prompted to ask this question to the .NET gurus out there: Is interface casting faster than class casting?
要给出一个code为例,假设它存在:
To give a code example, let's say this exists:
public interface IEntity { IParent DaddyMommy { get; } }
public interface IParent : IEntity { }
public class Parent : Entity, IParent { }
public class Entity : IEntity
{
public IParent DaddyMommy { get; protected set; }
public IParent AdamEve_Interfaces
{
get
{
IEntity e = this;
while (e.DaddyMommy != null)
e = e.DaddyMommy as IEntity;
return e as IParent;
}
}
public Parent AdamEve_Classes
{
get
{
Entity e = this;
while (e.DaddyMommy != null)
e = e.DaddyMommy as Entity;
return e as Parent;
}
}
}
那么,是AdamEve_Interfaces比AdamEve_Classes快?如果是的话,多少?而且,如果你知道答案,为什么呢?
So, is AdamEve_Interfaces faster than AdamEve_Classes? If so, by how much? And, if you know the answer, why?
推荐答案
看看这里:
<一个href="http://thatstoday.com/robbanp/blog/6/25/csharp-performance--cast-vs-interface">http://thatstoday.com/robbanp/blog/6/25/csharp-performance--cast-vs-interface
和,是的,你似乎是正确的。
And, yes, you seem to be right.
修改嗯,看来我错了。而像我的帕特里西奥Martinho费尔南德斯评论波纹管,上面的链接是完全假的(但我会继续在这里,为了诚实的编辑)。
Edit Well, it seems that I was wrong. And like my "patrício" Martinho Fernandes commented bellow, the above link is completely bogus (but I'll keep it here, for the sake of honest editing).
我有一些空闲时间的今天,所以我写了一个简单的性能测量code:
I do have some spare time nowadays, so I've written a simple performance measuring code:
public partial class Form1 : Form
{
private const int Cycles = 10000000;
public interface IMyInterface
{
int SameProperty { get; set; }
}
public class InterfacedClass : IMyInterface
{
public int SameProperty { get; set; }
}
public class SimpleClass
{
public int SameProperty { get; set; }
}
public struct InterfacedStruct : IMyInterface
{
public int SameProperty { get; set; }
}
public struct SimpleStruct
{
public int SameProperty { get; set; }
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
var simpleClassTime = MeasureSimpleClass();
var interfacedClassTime = MeasureInterfacedClass();
var simpleStructTime = MeasureSimpleStruct();
var interfacedStructTime = MeasureInterfacedStruct();
var message = string.Format(
"simpleClassTime = {0}\r\ninterfacedClassTime = {1}\r\nsimpleStructTime = {2}\r\ninterfacedStructTime = {3}",
simpleClassTime,
interfacedClassTime,
simpleStructTime,
interfacedStructTime
);
textBox.Text = message;
}
private static long MeasureSimpleClass() {
var watch = Stopwatch.StartNew();
var obj = new SimpleClass();
for (var i = 0; i < Cycles; i++)
{
obj.SameProperty = i;
var j = obj.SameProperty;
obj.SameProperty = j;
}
return watch.ElapsedMilliseconds;
}
private static long MeasureInterfacedClass() {
var watch = Stopwatch.StartNew();
IMyInterface obj = new InterfacedClass();
for (var i = 0; i < Cycles; i++) {
obj.SameProperty = i;
var j = obj.SameProperty;
obj.SameProperty = j;
}
return watch.ElapsedMilliseconds;
}
private static long MeasureSimpleStruct()
{
var watch = Stopwatch.StartNew();
var obj = new SimpleStruct();
for (var i = 0; i < Cycles; i++)
{
obj.SameProperty = i;
var j = obj.SameProperty;
obj.SameProperty = j;
}
return watch.ElapsedMilliseconds;
}
private static long MeasureInterfacedStruct()
{
var watch = Stopwatch.StartNew();
IMyInterface obj = new InterfacedStruct();
for (var i = 0; i < Cycles; i++)
{
obj.SameProperty = i;
var j = obj.SameProperty;
obj.SameProperty = j;
}
return watch.ElapsedMilliseconds;
}
}
和结果是:
simpleClassTime = 274
interfacedClassTime = 339
simpleStructTime = 247
interfacedStructTime = 302
我真的常想,一个接口将更快类
类型和速度较慢的结构
(自装箱/拆箱涉及后者),但事实并非如此。一个具体的类/结构参考是总是快,似乎
I really used to think that an interface would be faster for class
types, and slower for struct
(since boxing/unboxing is involved in the latter), but that is not the case: a concrete class/struct reference is always faster, it seems.
此外,为了敬启者:我相信,性能的不可以一个好决定是否应该或不应该使用的接口标准。所不同的是,就像其他人在这里说,可以忽略不计。
Also, to whom it may concern: I believe that performance is not a good criteria for deciding if an interface should or should not be used. The difference is, like others said here, negligible.
这篇关于接口与铸造类铸件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!