最近我阅读了关于《早期和经常性的性能测量,第2部分》的文章,它附带有源代码和二进制文件。
文章摘录:“我强调过,要可靠地创建高性能程序,你需要在设计过程中早期了解你使用的各个组件的性能。”
所以,我使用他的工具(版本为0.2.2)进行基准测试并尝试查看各个组件的性能。
在我的PC(x64)上,结果如下:
Name Median Mean StdDev Min Max Samples
NOTHING [count=1000] 0.14 0.177 0.164 0 0.651 10
MethodCalls: EmptyStaticFunction() [count=1000 scale=10.0] 1 1.005 0.017 0.991 1.042 10
Loop 1K times [count=1000] 85.116 85.312 0.392 84.93 86.279 10
MethodCalls: EmptyStaticFunction(arg1,...arg5) [count=1000 scale=10.0] 1.163 1.172 0.015 1.163 1.214 10
MethodCalls: aClass.EmptyInstanceFunction() [count=1000 scale=10.0] 1.009 1.011 0.019 0.995 1.047 10
MethodCalls: aClass.Interface() [count=1000 scale=10.0] 1.112 1.121 0.038 1.098 1.233 10
MethodCalls: aSealedClass.Interface() (inlined) [count=1000 scale=10.0] 0 0.008 0.025 0 0.084 10
MethodCalls: aStructWithInterface.Interface() (inlined) [count=1000 scale=10.0] 0 0.008 0.025 0 0.084 10
MethodCalls: aClass.VirtualMethod() [count=1000 scale=10.0] 0.674 0.683 0.025 0.674 0.758 10
MethodCalls: Class.ReturnsValueType() [count=1000 scale=10.0] 2.165 2.16 0.033 2.107 2.209 10
我惊讶地发现虚方法(0.674)比非虚实例方法(1.009)或静态方法(1)更快。而接口的速度也不慢!(我本来期望接口至少会慢2倍)。
由于这些结果来自可信的来源,我想知道如何解释上述发现。
我认为文章并没有过时的问题,因为在文章本身中并没有关于读数的任何说明。它所做的只是提供一个基准测试工具。