const和readonly哪个快?

Effictive C#, 50 Specific Ways to Improve Your C#一书提到,运行时常量readonly比编译时常量const更好。不是因为运行时常量readonly会比较快,而是因为这样的常量是运行时查找位置的,而不是编译时直接编译进IL,因此当版本出现交差覆盖的时候不会出错,也许会慢一点点,但是运行时常量readonly总是能够正确运行。
那么,运行时常量readonly到底比编译时常量const慢多少呢?
我写了个测试,框架就不介绍了。

[code=’c#’]
using System;
using System.Collections.Generic;
using System.Text;

namespace CSharpPerformance.TestCase
{
class ReadOnlyVsConst : TestCase
{
private const double ConstValue = 1.23456789;
private readonly double ReadOnlyValue = 1.23456789;

private double DoCalculate(double value, long count)
{
double result = 1;
for (int i = 0; i < count; i++) { result *= value; } return result; } public override void DoTest() { int count = 100000000; name1 = "Const: "; DateTime start = DateTime.Now; DoCalculate(ConstValue, count); DateTime end = DateTime.Now; span1 = end.Subtract(start); name2 = "ReadOnly: "; start = DateTime.Now; DoCalculate(ReadOnlyValue, count); end = DateTime.Now; span2 = end.Subtract(start); } } } [/code] 我执行了10遍,结果是这样的: Debug模式(Optimize code: ON):
[code=’c#’]
Const: 00:00:12.5802579
ReadOnly: 00:00:13.6913690
————————————
Const: 00:00:13.2063205
ReadOnly: 00:00:12.7162715
————————————
Const: 00:00:12.5372536
ReadOnly: 00:00:12.2612260
————————————
Const: 00:00:12.3152314
ReadOnly: 00:00:12.4542453
————————————
Const: 00:00:12.4342433
ReadOnly: 00:00:12.5922591
————————————
Const: 00:00:12.3752374
ReadOnly: 00:00:12.3442343
————————————
Const: 00:00:12.1912190
ReadOnly: 00:00:12.1782177
————————————
Const: 00:00:12.1932192
ReadOnly: 00:00:12.2582257
————————————
Const: 00:00:12.2472246
ReadOnly: 00:00:12.2532252
————————————
Const: 00:00:12.2462245
ReadOnly: 00:00:12.3122311
————————————
[/code]
结果:const VS readonly:6:4

Debug模式(Optimize code: OFF):
[code=’c#’]
Const: 00:00:12.4402439
ReadOnly: 00:00:12.3002299
————————————
Const: 00:00:12.2512250
ReadOnly: 00:00:12.2272226
————————————
Const: 00:00:12.2302229
ReadOnly: 00:00:12.1962195
————————————
Const: 00:00:12.2922291
ReadOnly: 00:00:12.2152214
————————————
Const: 00:00:12.3062305
ReadOnly: 00:00:12.1942193
————————————
Const: 00:00:12.2012200
ReadOnly: 00:00:12.2252224
————————————
Const: 00:00:12.1922191
ReadOnly: 00:00:12.2402239
————————————
Const: 00:00:12.2602259
ReadOnly: 00:00:12.2452244
————————————
Const: 00:00:12.2322231
ReadOnly: 00:00:12.1952194
————————————
Const: 00:00:12.2492248
ReadOnly: 00:00:12.1912190
————————————
[/code]
结果:const VS readonly:2:8,Shocking!

Release模式(Optimize code: ON):
[code=’c#’]
Const: 00:00:08.7728772
ReadOnly: 00:00:08.8708870
————————————
Const: 00:00:08.7698769
ReadOnly: 00:00:08.7088708
————————————
Const: 00:00:08.7148714
ReadOnly: 00:00:08.6878687
————————————
Const: 00:00:08.7258725
ReadOnly: 00:00:08.7758775
————————————
Const: 00:00:08.8258825
ReadOnly: 00:00:08.7748774
————————————
Const: 00:00:08.8948894
ReadOnly: 00:00:08.8378837
————————————
Const: 00:00:08.7198719
ReadOnly: 00:00:08.8348834
————————————
Const: 00:00:08.7338733
ReadOnly: 00:00:08.7398739
————————————
Const: 00:00:08.7138713
ReadOnly: 00:00:08.7688768
————————————
Const: 00:00:08.7948794
ReadOnly: 00:00:08.7698769
————————————
[/code]
结果:const VS readonly:5:5

Release模式(Optimize code: OFF):
[code=’c#’]
Const: 00:00:08.9068906
ReadOnly: 00:00:08.9008900
————————————
Const: 00:00:08.9108910
ReadOnly: 00:00:08.7988798
————————————
Const: 00:00:08.8098809
ReadOnly: 00:00:08.7978797
————————————
Const: 00:00:08.8048804
ReadOnly: 00:00:08.8218821
————————————
Const: 00:00:08.9528952
ReadOnly: 00:00:08.8898889
————————————
Const: 00:00:08.9138913
ReadOnly: 00:00:09.0019001
————————————
Const: 00:00:08.9288928
ReadOnly: 00:00:08.7968796
————————————
Const: 00:00:08.7798779
ReadOnly: 00:00:08.8298829
————————————
Const: 00:00:08.8258825
ReadOnly: 00:00:08.7558755
————————————
Const: 00:00:08.8138813
ReadOnly: 00:00:08.8168816
————————————
[/code]
结果:const VS readonly:4:6

数数看,理论上const应该是绝对快的,其实不然。编译优化关闭的时候,const居然落后于readonly。

推荐文章