问题正如 Azodious 提到的,您不能使用调试模式来测量时间,因为它不准确。
打开发布模式后,我得到以下数字:
递增k
:445
编号:402
IL
递增版本中还有 4条指令:
IL_0001: ldc.i4.0
IL_0002: stloc.0
IL_0003: ldc.i4.0
IL_0004: stloc.1
IL_0005: br.s IL_003B
IL_0007: ldc.i4.0
IL_0008: stloc.2
IL_0009: br.s IL_0029
IL_000B: ldc.i4.0
IL_000C: stloc.3
IL_000D: br.s IL_0017
IL_000F: ldloc.0
IL_0010: ldc.i4.1
IL_0011: add
IL_0012: stloc.0
IL_0013: ldloc.3
IL_0014: ldc.i4.1
IL_0015: add
IL_0016: stloc.3
IL_0017: ldloc.3
IL_0018: ldc.i4 E8 03 00 00
IL_001D: clt
IL_001F: stloc.s 04
IL_0021: ldloc.s 04
IL_0023: brtrue.s IL_000F
IL_0025: ldloc.2
IL_0026: ldc.i4.1
IL_0027: add
IL_0028: stloc.2
IL_0029: ldloc.2
IL_002A: ldc.i4 E8 03 00 00
IL_002F: clt
IL_0031: stloc.s 04
IL_0033: ldloc.s 04
IL_0035: brtrue.s IL_000B
IL_0037: ldloc.1
IL_0038: ldc.i4.1
IL_0039: add
IL_003A: stloc.1
IL_003B: ldloc.1
IL_003C: ldc.i4 E8 03 00 00
IL_0041: clt
IL_0043: stloc.s 04
IL_0045: ldloc.s 04
IL_0047: brtrue.s IL_0007
-verisonNOP
有相同数量的分支,但少一个add
:
IL_0001: ldc.i4.0
IL_0002: stloc.0
IL_0003: ldc.i4.0
IL_0004: stloc.1
IL_0005: br.s IL_0037
IL_0007: ldc.i4.0
IL_0008: stloc.2
IL_0009: br.s IL_0025
IL_000B: ldc.i4.0
IL_000C: stloc.3
IL_000D: br.s IL_0013
IL_000F: ldloc.3
IL_0010: ldc.i4.1
IL_0011: add
IL_0012: stloc.3
IL_0013: ldloc.3
IL_0014: ldc.i4 E8 03 00 00
IL_0019: clt
IL_001B: stloc.s 04
IL_001D: ldloc.s 04
IL_001F: brtrue.s IL_000F
IL_0021: ldloc.2
IL_0022: ldc.i4.1
IL_0023: add
IL_0024: stloc.2
IL_0025: ldloc.2
IL_0026: ldc.i4 E8 03 00 00
IL_002B: clt
IL_002D: stloc.s 04
IL_002F: ldloc.s 04
IL_0031: brtrue.s IL_000B
IL_0033: ldloc.1
IL_0034: ldc.i4.1
IL_0035: add
IL_0036: stloc.1
IL_0037: ldloc.1
IL_0038: ldc.i4 E8 03 00 00
IL_003D: clt
IL_003F: stloc.s 04
IL_0041: ldloc.s 04
IL_0043: brtrue.s IL_0007
这些是在没有优化的情况下编译的,因为我想看看到底发生了什么。
实际上,它们之间的唯一区别是:
IL_0012: stloc.0
IL_0013: ldloc.3
IL_0014: ldc.i4.1
IL_0015: add
简单地说:你得到奇怪的数字,因为你处于调试模式。