代码之家  ›  专栏  ›  技术社区  ›  ilitirit

是vs类型的吗

  •  136
  • ilitirit  · 技术社区  · 18 年前

    if (obj is ClassA) {}
    
    if (obj.GetType() == typeof(ClassA)) {}
    

    编辑:

    4 回复  |  直到 18 年前
        1
  •  198
  •   Jay Bazuzi Buck Hodges    16 年前

    如果他们不做同样的事情,哪一个更快重要吗?比较具有不同含义的语句的性能似乎不是一个好主意。

    is 告诉您对象是否实现了 ClassA GetType() 告诉您最派生的类型。

    不一样。

        2
  •  169
  •   oliver4888 MagicKat    6 年前

    This should answer that question, and then some.

    if (obj.GetType() == typeof(ClassA)) {} ,对于那些不想读这篇文章的人来说,速度更快。

    (请注意,它们不会做相同的事情)

        3
  •  27
  •   Community Mohan Dere    9 年前
        4
  •  18
  •   nawfal Donny V.    13 年前

    我做了一些基准测试,他们做的是相同的密封类型。

    var c1 = "";
    var c2 = typeof(string);
    object oc1 = c1;
    object oc2 = c2;
    
    var s1 = 0;
    var s2 = '.';
    object os1 = s1;
    object os2 = s2;
    
    bool b = false;
    
    Stopwatch sw = Stopwatch.StartNew();
    for (int i = 0; i < 10000000; i++)
    {
        b = c1.GetType() == typeof(string); // ~60ms
        b = c1 is string; // ~60ms
    
        b = c2.GetType() == typeof(string); // ~60ms
        b = c2 is string; // ~50ms
    
        b = oc1.GetType() == typeof(string); // ~60ms
        b = oc1 is string; // ~68ms
    
        b = oc2.GetType() == typeof(string); // ~60ms
        b = oc2 is string; // ~64ms
    
    
        b = s1.GetType() == typeof(int); // ~130ms
        b = s1 is int; // ~50ms
    
        b = s2.GetType() == typeof(int); // ~140ms
        b = s2 is int; // ~50ms
    
        b = os1.GetType() == typeof(int); // ~60ms
        b = os1 is int; // ~74ms
    
        b = os2.GetType() == typeof(int); // ~60ms
        b = os2 is int; // ~68ms
    
    
        b = GetType1<string, string>(c1); // ~178ms
        b = GetType2<string, string>(c1); // ~94ms
        b = Is<string, string>(c1); // ~70ms
    
        b = GetType1<string, Type>(c2); // ~178ms
        b = GetType2<string, Type>(c2); // ~96ms
        b = Is<string, Type>(c2); // ~65ms
    
        b = GetType1<string, object>(oc1); // ~190ms
        b = Is<string, object>(oc1); // ~69ms
    
        b = GetType1<string, object>(oc2); // ~180ms
        b = Is<string, object>(oc2); // ~64ms
    
    
        b = GetType1<int, int>(s1); // ~230ms
        b = GetType2<int, int>(s1); // ~75ms
        b = Is<int, int>(s1); // ~136ms
    
        b = GetType1<int, char>(s2); // ~238ms
        b = GetType2<int, char>(s2); // ~69ms
        b = Is<int, char>(s2); // ~142ms
    
        b = GetType1<int, object>(os1); // ~178ms
        b = Is<int, object>(os1); // ~69ms
    
        b = GetType1<int, object>(os2); // ~178ms
        b = Is<int, object>(os2); // ~69ms
    }
    
    sw.Stop();
    MessageBox.Show(sw.Elapsed.TotalMilliseconds.ToString());
    

    static bool GetType1<S, T>(T t)
    {
        return t.GetType() == typeof(S);
    }
    static bool GetType2<S, T>(T t)
    {
        return typeof(T) == typeof(S);
    }
    static bool Is<S, T>(T t)
    {
        return t is S;
    }
    

    我也尝试了自定义类型,结果是一致的:

    var c1 = new Class1();
    var c2 = new Class2();
    object oc1 = c1;
    object oc2 = c2;
    
    var s1 = new Struct1();
    var s2 = new Struct2();
    object os1 = s1;
    object os2 = s2;
    
    bool b = false;
    
    Stopwatch sw = Stopwatch.StartNew();
    for (int i = 0; i < 10000000; i++)
    {
        b = c1.GetType() == typeof(Class1); // ~60ms
        b = c1 is Class1; // ~60ms
    
        b = c2.GetType() == typeof(Class1); // ~60ms
        b = c2 is Class1; // ~55ms
    
        b = oc1.GetType() == typeof(Class1); // ~60ms
        b = oc1 is Class1; // ~68ms
    
        b = oc2.GetType() == typeof(Class1); // ~60ms
        b = oc2 is Class1; // ~68ms
    
    
        b = s1.GetType() == typeof(Struct1); // ~150ms
        b = s1 is Struct1; // ~50ms
    
        b = s2.GetType() == typeof(Struct1); // ~150ms
        b = s2 is Struct1; // ~50ms
    
        b = os1.GetType() == typeof(Struct1); // ~60ms
        b = os1 is Struct1; // ~64ms
    
        b = os2.GetType() == typeof(Struct1); // ~60ms
        b = os2 is Struct1; // ~64ms
    
    
        b = GetType1<Class1, Class1>(c1); // ~178ms
        b = GetType2<Class1, Class1>(c1); // ~98ms
        b = Is<Class1, Class1>(c1); // ~78ms
    
        b = GetType1<Class1, Class2>(c2); // ~178ms
        b = GetType2<Class1, Class2>(c2); // ~96ms
        b = Is<Class1, Class2>(c2); // ~69ms
    
        b = GetType1<Class1, object>(oc1); // ~178ms
        b = Is<Class1, object>(oc1); // ~69ms
    
        b = GetType1<Class1, object>(oc2); // ~178ms
        b = Is<Class1, object>(oc2); // ~69ms
    
    
        b = GetType1<Struct1, Struct1>(s1); // ~272ms
        b = GetType2<Struct1, Struct1>(s1); // ~140ms
        b = Is<Struct1, Struct1>(s1); // ~163ms
    
        b = GetType1<Struct1, Struct2>(s2); // ~272ms
        b = GetType2<Struct1, Struct2>(s2); // ~140ms
        b = Is<Struct1, Struct2>(s2); // ~163ms
    
        b = GetType1<Struct1, object>(os1); // ~178ms
        b = Is<Struct1, object>(os1); // ~64ms
    
        b = GetType1<Struct1, object>(os2); // ~178ms
        b = Is<Struct1, object>(os2); // ~64ms
    }
    
    sw.Stop();
    MessageBox.Show(sw.Elapsed.TotalMilliseconds.ToString());
    

    以及类型:

    sealed class Class1 { }
    sealed class Class2 { }
    struct Struct1 { }
    struct Struct2 { }
    

    1. 使命感 GetType 在…上 struct s比较慢。 GetType 定义在 object 结构 s需要装箱才能打电话 GetType .

    2. GetType 速度更快,但幅度很小。

    3. T 是 class 然后 is T 是 是 比以前快多了 GetType 但是 typeof(T) 比两者都快得多。 在 T 班 , 类型(T) t.GetType

    简而言之,如果你有 GetType . 如果你有一个通用的 班 类型、用途 . 如果你有一个通用的 结构 类型、用途 类型(T) 是 . 如果希望始终与一种样式保持一致(对于密封类型),请使用 是 ..