类型转换的性能 [英] Performance of TypeCasting
问题描述
有
((TypeA) obj).method1();
((TypeA) obj).method2();
((TypeA) obj).method3();
和
var A = (TypeA) obj;
A.method1();
A.method2();
A.method3();
使用很多时候?
when used alot of times?
我经常看到类似
if (((TextBox)sender).Text.Contains('.') || ((TextBox)sender).Text.Contains(','))
和不知道这是一种浪费的表现。
and wonder if this is a waste of performance.
推荐答案
这可能是可衡量的,如果它正在做几十亿次用很少的其他工作。我不知道CLR是否会有效地缓存,中投工作的事实,所以它并不需要再次做到这一点 - 如果它没有这样做,现在,它可能会在以后的版本这样做。它可能这样做,在64位的JIT但不是32位版本,或反之亦然 - 你的想法。我怀疑这会使得在正常code一显著的差异,但。
It may be measurable if it's being done billions of times with very little other work. I don't know whether the CLR will effectively cache the fact that the cast worked, so it doesn't need to do it again - if it doesn't do so now, it might do so in a later release. It might do so in the 64 bit JIT but not the 32 bit version, or vice versa - you get the idea. I doubt that it would make a significant difference in normal code though.
我个人喜欢的的可读性第二种形式的多,虽然,这是更重要的是迄今为止。
Personally I like the readability of the second form more though, and that's more important by far.
这篇关于类型转换的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!