为什么运营商'as'比'is'慢?

时间:2013-02-25 10:03:03

标签: c# reflection operators

我有下一堂课:

public class A
{
    public int MyProperty { get; set; }
}

以下代码在Main:

object obj = new A();

Stopwatch sw = Stopwatch.StartNew();
var res = obj as A;
if (res != null)
{
    res.MyProperty = 10;
    Console.WriteLine("obj is A (as)" + sw.Elapsed);
}
sw.Stop();

Stopwatch sw2 = Stopwatch.StartNew();
if (obj.GetType() == typeof(A))
{
    A a = (A)obj;
    a.MyProperty = 10;
    Console.WriteLine("obj is A (GetType)" + sw2.Elapsed);
}
sw2.Stop();

Stopwatch sw3 = Stopwatch.StartNew();
var isA = obj is A;
if (isA)
{
    A a = (A)obj;
    a.MyProperty = 19;
    Console.WriteLine("obj is A (is)" + sw3.Elapsed);
}
sw3.Stop();

Console.ReadKey();

结果是:

obj is A (as) 00:00:00.0000589
obj is A (GetType) 00:00:00.0000024
obj is A (is) 00:00:00.0000006

关键在于运算符'is'的工作总是比'as'快。为什么'as'比'is'慢?甚至比'as'更快的GetType()。与'is'和GetType()相比,什么代表'as'运算符导致此类延迟。

3 个答案:

答案 0 :(得分:4)

我猜这是第一个必须打开流到控制台的Console.Write,因此需要花费更多时间。

无论如何,写入控制台所需的时间比进行演员要多得多,所以你无法得出任何关于测试中铸件的结论。

进行十亿次演员表并且不为每个演员阵容向控制台写任何东西,给你一个更合理的结果:

object obj = new A();
int iterations = 1000000000;

Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < iterations; i++) {
  var res = obj as A;
  if (res != null) {
    res.MyProperty = 10;
  }
}
sw.Stop();
Console.WriteLine("obj is A (as)" + sw.Elapsed);

Stopwatch sw2 = Stopwatch.StartNew();
for (int i = 0; i < iterations; i++) {
  if (obj.GetType() == typeof(A)) {
    A a = (A)obj;
    a.MyProperty = 10;
  }
}
sw2.Stop();
Console.WriteLine("obj is A (GetType)" + sw2.Elapsed);

Stopwatch sw3 = Stopwatch.StartNew();
for (int i = 0; i < iterations; i++) {
  var isA = obj is A;
  if (isA) {
    A a = (A)obj;
    a.MyProperty = 19;
  }
}
sw3.Stop();
Console.WriteLine("obj is A (is)" + sw3.Elapsed);

示例输出:

obj is A (as)00:00:00.3937249
obj is A (GetType)00:00:00.3452988
obj is A (is)00:00:01.0193541

答案 1 :(得分:2)

如果他们显示“as”比“is”慢,那么您的测量值是不正确的。

不可能的原因是“as”和“is”关键字都生成isinst IL指令,但“is”指令会生成额外的返回值检查。

您无需执行任何计时来确定这一点,因为您可以使用反射器检查生成的IL代码。

例如,这种方法:

static bool isTest(object value)
{
    return value is Random;
}

生成此IL:

.method private hidebysig static bool isTest(object 'value') cil managed
{
    .maxstack 8
    L_0000: ldarg.0 
    L_0001: isinst [mscorlib]System.Random
    L_0006: ldnull 
    L_0007: cgt.un 
    L_0009: ret 
}

虽然这个:

static object asTest(object value)
{
    return value as Random;
}

生成:

.method private hidebysig static object asTest(object 'value') cil managed
{
    .maxstack 8
    L_0000: ldarg.0 
    L_0001: isinst [mscorlib]System.Random
    L_0006: ret 
}

“is”关键字生成与“as”关键字相同的IL以及一些其他指令;因此,对于这种用法,“是”必须慢于“as”。

答案 2 :(得分:1)

我认为这可能是因为您还设置了MyProperty,所以第一次这样做时,属性设置器就是JIT编译的。

尝试使用和不使用注释行运行此代码,并检查区别:

    object obj = new A();

    // uncomment these lines and see the difference
    // A tmp = new A();
    // tmp.MyProperty = 100;

    Stopwatch sw = Stopwatch.StartNew();
    var res = obj as A;
    if (res != null) {
        res.MyProperty = 10;
    }
    sw.Stop();
    Console.WriteLine("as : " + sw.Elapsed);

    Stopwatch sw2 = Stopwatch.StartNew();
    if (obj.GetType() == typeof(A)) {
        A a = (A)obj;
        a.MyProperty = 10;
    }
    sw2.Stop();
    Console.WriteLine("GetType : " + sw2.Elapsed);

    Stopwatch sw3 = Stopwatch.StartNew();
    var isA = obj is A;
    if (isA) {
        A a = (A)obj;
        a.MyProperty = 19;
    }
    sw3.Stop();
    Console.WriteLine("is : " + sw3.Elapsed);