is 和 typeof 哪个更快
这些代码中哪一段更快?
if (obj is ClassA) {}
if (obj.GetType() == typeof(ClassA)) {}
编辑: 我知道他们不做同样的事情。
Which of these pieces of code is faster?
if (obj is ClassA) {}
if (obj.GetType() == typeof(ClassA)) {}
Edit:
I'm aware that they don't do the same thing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
他们不做同样的事情。 如果 obj 属于 ClassA 类型或 ClassA 的某个子类,则第一个方法有效。 第二个仅匹配 ClassA 类型的对象。 第二个会更快,因为它不必检查类层次结构。
对于那些想知道原因但又不想阅读 中引用的文章的人是 vs typeof。
They don't do the same thing. The first one works if obj is of type ClassA or of some subclass of ClassA. The second one will only match objects of type ClassA. The second one will be faster since it doesn't have to check the class hierarchy.
For those who want to know the reason, but don't want to read the article referenced in is vs typeof.
我做了一些基准测试,他们做了同样的事情——密封类型。
用于测试泛型类型的泛型函数:
我也尝试了自定义类型,结果是一致的:
并且类型:
推理:
在
structGetType
code>s 速度较慢。GetType
是在object
类上定义的,不能在子类型中重写,因此struct
s需要装箱才能调用GetType
。在对象实例上,
GetType
速度更快,但速度非常快。在泛型类型上,如果
T
是class
,则is
是快多了。 如果T
是struct
,则is
比GetType
快得多,但typeof(T) 比两者都快得多。
在
T
是class
的情况下,typeof(T)
不可靠,因为它不同来自实际基础类型t.GetType
。简而言之,如果您有一个
object
实例,请使用GetType
。 如果您有通用的class
类型,请使用is
。 如果您有通用的struct
类型,请使用typeof(T)
。 如果您不确定泛型类型是引用类型还是值类型,请使用is
。 如果您希望始终与一种样式保持一致(对于密封类型),请使用is
..I did some benchmarking where they do the same - sealed types.
The generic functions to test for generic types:
I tried for custom types as well and the results were consistent:
And the types:
Inference:
Calling
GetType
onstruct
s is slower.GetType
is defined onobject
class which can't be overridden in sub types and thusstruct
s need to be boxed to be calledGetType
.On an object instance,
GetType
is faster, but very marginally.On generic type, if
T
isclass
, thenis
is much faster. IfT
isstruct
, thenis
is much faster thanGetType
buttypeof(T)
is much faster than both. In cases ofT
beingclass
,typeof(T)
is not reliable since its different from actual underlying typet.GetType
.In short, if you have an
object
instance, useGetType
. If you have a genericclass
type, useis
. If you have a genericstruct
type, usetypeof(T)
. If you are unsure if generic type is reference type or value type, useis
. If you want to be consistent with one style always (for sealed types), useis
..这应该回答这个问题,然后是一些。
第二行
if (obj.GetType() == typeof(ClassA)) {}
对于那些不想阅读文章。(请注意,他们不做同样的事情)
This should answer that question, and then some.
The second line,
if (obj.GetType() == typeof(ClassA)) {}
, is faster, for those that don't want to read the article.(Be aware that they don't do the same thing)
如果他们不做同样的事情,哪个更快还有关系吗? 比较具有不同含义的语句的性能似乎是一个坏主意。
is
告诉您该对象是否在其类型层次结构中的任何位置实现了ClassA
。GetType()
告诉您最派生的类型。不是同一件事。
Does it matter which is faster, if they don't do the same thing? Comparing the performance of statements with different meaning seems like a bad idea.
is
tells you if the object implementsClassA
anywhere in its type heirarchy.GetType()
tells you about the most-derived type.Not the same thing.