为科学做了测试:
版本 1
public void Test1()
{
decimal[][] originalFormTableau = CreateOriginalFormTableau();
decimal[][] standardFormTableau = InsertSlackVariables(originalFormTableau);
this.Engine = new SimplexEngine(standardFormTableau, normalizationThreshold);
}
输出:
.method public hidebysig instance void Test1() cil managed
{
.maxstack 4
.locals init (
[0] valuetype [mscorlib]System.Decimal[][] originalFormTableau,
[1] valuetype [mscorlib]System.Decimal[][] standardFormTableau)
L_0000: ldarg.0
L_0001: call instance valuetype [mscorlib]System.Decimal[][] ConsoleApplication2.Class2::CreateOriginalFormTableau()
L_0006: stloc.0
L_0007: ldarg.0
L_0008: ldloc.0
L_0009: call instance valuetype [mscorlib]System.Decimal[][] ConsoleApplication2.Class2::InsertSlackVariables(valuetype [mscorlib]System.Decimal[][])
L_000e: stloc.1
L_000f: ldarg.0
L_0010: ldloc.1
L_0011: ldarg.0
L_0012: call instance object ConsoleApplication2.Class2::get_normalizationThreshold()
L_0017: newobj instance void ConsoleApplication2.SimplexEngine::.ctor(valuetype [mscorlib]System.Decimal[][], object)
L_001c: call instance void ConsoleApplication2.Class2::set_Engine(class ConsoleApplication2.SimplexEngine)
L_0021: ret
}
版本 2
this.Engine = new SimplexEngine(
InsertSlackVariables(
CreateOriginalFormTableau()),
normalizationThreshold);
输出:
.method public hidebysig instance void Test2() cil managed
{
.maxstack 8
L_0000: ldarg.0
L_0001: ldarg.0
L_0002: ldarg.0
L_0003: call instance valuetype [mscorlib]System.Decimal[][] ConsoleApplication2.Class2::CreateOriginalFormTableau()
L_0008: call instance valuetype [mscorlib]System.Decimal[][] ConsoleApplication2.Class2::InsertSlackVariables(valuetype [mscorlib]System.Decimal[][])
L_000d: ldarg.0
L_000e: call instance object ConsoleApplication2.Class2::get_normalizationThreshold()
L_0013: newobj instance void ConsoleApplication2.SimplexEngine::.ctor(valuetype [mscorlib]System.Decimal[][], object)
L_0018: call instance void ConsoleApplication2.Class2::set_Engine(class ConsoleApplication2.SimplexEngine)
L_001d: ret
}
在发行版中编译,优化开启。我不得不说,由于方法中没有重用局部变量,我相信编译器会重写Test1
为Test2
. 不知何故,它没有。所以我想Test2
会稍微快一点,尽管它可能不是以可衡量的方式。
因此,除非您正在搜索微秒优化,否则请选择最易读的优化。