TypeCasting的性能

是否有任何可衡量的性能差异

((TypeA) obj).method1(); ((TypeA) obj).method2(); ((TypeA) obj).method3(); 

 var A = (TypeA) obj; A.method1(); A.method2(); A.method3(); 

当使用很多次?

我经常看到类似的东西

 if (((TextBox)sender).Text.Contains('.') || ((TextBox)sender).Text.Contains(',')) 

并想知道这是否浪费表演。

如果数十亿次,而其他工作很less,这可能是可以衡量的。 我不知道CLR是否会有效地caching演员工作的事实,因此不需要再次这样做 – 如果现在不这样做,它可能会在以后的版本中这样做。 它可能会在64位的JIT,但不是在32位版本,反之亦然 – 你明白了。 我怀疑它会在正常的代码有重大的不同。

就我个人而言,我更喜欢第二种forms的可读性 ,而这一点至关重要。

@dkson:我testing了这两种方法。 以下是我在电脑上find的内容:

他们的performance大致相同 。 事实上,我发现第二种方法会稍微慢一些。 原因(我相信)是额外的variables和初始投的成本。 当然,如果你使用足够的演员,你可能会得到性能损失。 看起来你只有在节省20到30次之后才能在performance上达到平衡。

以下是最近两次testing的结果:

 TestMuliCast\_3x: 00:00:00.5970000 TestSingleCast\_3x: 00:00:00.6020000 TestMuliCast\_30x: 00:00:06.0930000 TestSingleCast\_30x: 00:00:06.0480000 TestMuliCast\_3x: 00:00:00.6120000 TestSingleCast\_3x: 00:00:00.6250000 TestMuliCast\_30x: 00:00:06.5490000 TestSingleCast\_30x: 00:00:06.4440000 

我也testing了castclassisinst之间的区别。 基于我读过的内容:

http://m3mia.blogspot.com/2007/11/comparing-isinst-to-castclass.html
http://www.codeproject.com/KB/cs/csharpcasts.aspx
http://discuss.joelonsoftware.com/default.asp?dotnet.12.635066.13

即使没有例外,我认为isinst会比castclass快。 然而,在创build我自己的基准之后,我发现isinst比castclass稍微一些。 很有意思。 这是我的结果:

 TestEmptyLoop: 00:00:00.0870000 TestDCast\_castclass: 00:00:00.2640000 TestDCast\_isinst: 00:00:00.3780000 TestEmptyLoop: 00:00:00.0870000 TestDCast\_castclass: 00:00:00.2600000 TestDCast\_isinst: 00:00:00.3750000 

所以,Skeet先生,我纠正了。

环境:

Windows Vista
最高核心速度3.2Ghz
.NET Framework v2.0.50727

以下是我创build和运行的基准testing的完整源代码(使用Jon Skeets Microbenchmarking框架)

 using System; using System.Collections; public class CastingBenchmark { static Int64 Iterations=100000000; static Int64 TestWork = 0; public static void Init(string[] args) { if (args.Length>0) Iterations = Int64.Parse(args[0]); } public static void Reset() { TestWork = 0; } internal class BaseType { public void TestBaseMethod() { TestWork++; } } internal class DerivedType : BaseType { public void TestDerivedMethod() { TestWork++; } public void TestDerivedMethod2() { TestWork++; } public void TestDerivedMethod3() { TestWork++; } } [Benchmark] public static void TestMuliCast_3x() { BaseType TestBaseType = new DerivedType(); for (int x = 0; x < Iterations; x++) { ((DerivedType)TestBaseType).TestDerivedMethod(); ((DerivedType)TestBaseType).TestDerivedMethod2(); ((DerivedType)TestBaseType).TestDerivedMethod3(); } } [Benchmark] public static void TestSingleCast_3x() { BaseType TestBaseType = new DerivedType(); for (int x = 0; x < Iterations; x++) { DerivedType TestDerivedType = (DerivedType)TestBaseType; TestDerivedType.TestDerivedMethod(); TestDerivedType.TestDerivedMethod2(); TestDerivedType.TestDerivedMethod3(); } } [Benchmark] public static void TestMuliCast_30x() { BaseType TestBaseType = new DerivedType(); for (int x = 0; x < Iterations; x++) { //Simulate 3 x 10 method calls while casting for (int y = 0; y < 10; y++) { ((DerivedType)TestBaseType).TestDerivedMethod(); ((DerivedType)TestBaseType).TestDerivedMethod2(); ((DerivedType)TestBaseType).TestDerivedMethod3(); } } } [Benchmark] public static void TestSingleCast_30x() { BaseType TestBaseType = new DerivedType(); for (int x = 0; x < Iterations; x++) { DerivedType TestDerivedType = (DerivedType)TestBaseType; //Simulate 3 x 10 method calls on already-cast object for (int y = 0; y < 10; y++) { TestDerivedType.TestDerivedMethod(); TestDerivedType.TestDerivedMethod2(); TestDerivedType.TestDerivedMethod3(); } } } [Benchmark] public static void TestEmptyLoop() { for (int x = 0; x < Iterations; x++) { } } [Benchmark] public static void TestDCast_castclass() { BaseType TestDerivedType = new DerivedType(); for (int x = 0; x < Iterations; x++) { ((DerivedType)TestDerivedType).TestDerivedMethod(); } } [Benchmark] public static void TestDCast_isinst() { BaseType TestDerivedType = new DerivedType(); for (int x = 0; x < Iterations; x++) { (TestDerivedType as DerivedType).TestDerivedMethod(); } } } 

以及isinstcastclass方法的结果IL:

 method public hidebysig static void TestDCast_isinst() cil managed { .custom instance void BenchmarkAttribute::.ctor() .maxstack 2 .locals init ( [0] class CastingBenchmark/BaseType TestDerivedType, [1] int32 x) L_0000: newobj instance void CastingBenchmark/DerivedType::.ctor() L_0005: stloc.0 L_0006: ldc.i4.0 L_0007: stloc.1 L_0008: br.s L_0019 L_000a: ldloc.0 L_000b: isinst CastingBenchmark/DerivedType L_0010: callvirt instance void CastingBenchmark/DerivedType::TestDerivedMethod() L_0015: ldloc.1 L_0016: ldc.i4.1 L_0017: add L_0018: stloc.1 L_0019: ldloc.1 L_001a: conv.i8 L_001b: ldsfld int64 CastingBenchmark::Iterations L_0020: blt.s L_000a L_0022: ret } .method public hidebysig static void TestDCast_castclass() cil managed { .custom instance void BenchmarkAttribute::.ctor() .maxstack 2 .locals init ( [0] class CastingBenchmark/BaseType TestDerivedType, [1] int32 x) L_0000: newobj instance void CastingBenchmark/DerivedType::.ctor() L_0005: stloc.0 L_0006: ldc.i4.0 L_0007: stloc.1 L_0008: br.s L_0019 L_000a: ldloc.0 L_000b: castclass CastingBenchmark/DerivedType L_0010: callvirt instance void CastingBenchmark/DerivedType::TestDerivedMethod() L_0015: ldloc.1 L_0016: ldc.i4.1 L_0017: add L_0018: stloc.1 L_0019: ldloc.1 L_001a: conv.i8 L_001b: ldsfld int64 CastingBenchmark::Iterations L_0020: blt.s L_000a L_0022: ret }