正如 Stringer Bell 提到的那样,DivRem 其中is not optimized 最高支持 .NET 3.5。
在 .NET 4.0 it uses NGen.
我使用Math.DivRem 得到的结果(调试;发布 = ~11000ms)
11863
11820
11881
11859
11854
我使用 MyDivRem 得到的结果(调试;发布 = ~11000ms)
29177
29214
29472
29277
29196
针对 x86 的项目。
Math.DivRem 用法示例
int mod1;
int div1 = Math.DivRem(4, 2, out mod1);
方法签名
DivRem(Int32, Int32, Int32&) : Int32
DivRem(Int64, Int64, Int64&) : Int64
.NET 4.0 代码
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static int DivRem(int a, int b, out int result)
{
result = a % b;
return (a / b);
}
.NET 4.0 IL
.custom instance void System.Runtime.TargetedPatchingOptOutAttribute::.ctor(string) = { string('Performance critical to inline across NGen image boundaries') }
.maxstack 8
L_0000: ldarg.2
L_0001: ldarg.0
L_0002: ldarg.1
L_0003: rem
L_0004: stind.i4
L_0005: ldarg.0
L_0006: ldarg.1
L_0007: div
L_0008: ret
MSDN Reference