C# 与x27之间的性能差异;变量==true';vs';真==变量';
这需要关闭/删除,因为差异不是他们的 这是我自己的错。但我不想删除我自己的问题 因为我会被封锁。因此,如果有一个主持人可以做到这一点 对我来说 我希望有人能告诉我两者的性能差异是什么C# 与x27之间的性能差异;变量==true';vs';真==变量';,c#,performance,C#,Performance,这需要关闭/删除,因为差异不是他们的 这是我自己的错。但我不想删除我自己的问题 因为我会被封锁。因此,如果有一个主持人可以做到这一点 对我来说 我希望有人能告诉我两者的性能差异是什么 variable==truevstrue==variable 经过一些测试,我发现true==variable的速度几乎是variable==true的两倍,这可能完全不相关,但我只是想知道 但经过几次跑步后,我看到了以下结果: 'variableTrue==true'与'true==variableTrue'的性
variable==true
vstrue==variable
经过一些测试,我发现true==variable
的速度几乎是variable==true
的两倍,这可能完全不相关,但我只是想知道
但经过几次跑步后,我看到了以下结果:
'variableTrue==true'与'true==variableTrue'的性能测试
Performance for 'variableTrue == true'
Average: 0.333442949999999 ms
Min: 0.2784 ms
Max: 7.6198 ms
Performance for 'variableTrue == false'
Average: 0.330602419999998 ms
Min: 0.2784 ms
Max: 10.2835 ms
Performance for 'true == variableTrue'
Average: 0.668567990000006 ms
Min: 0.5568 ms
Max: 7.9245 ms
Performance for 'false == variableTrue'
Average: 0.660314390000008 ms
Min: 0.5568 ms
Max: 10.5792 ms
'variableFalse==false'与'false==variableFalse'的性能测试
Performance for 'variableFalse == true'
Average: 0.31614 ms
Min: 0.295 ms
Max: 0.4047 ms
Performance for 'variableFalse == false'
Average: 0.29688 ms
Min: 0.2947 ms
Max: 0.3082 ms
Performance for 'true == variableFalse'
Average: 0.74112 ms
Min: 0.5901 ms
Max: 1.9459 ms
Performance for 'false == variableFalse'
Average: 0.62093 ms
Min: 0.5895 ms
Max: 0.781 ms
Press any key to continue
我测试它的代码可以在这里找到
没有区别。原因如下: 代码: 生成的内容:
// Fields
.field private bool 'value'
// Methods
.method public hidebysig
instance void M () cil managed
{
// Method begins at RVA 0x2050
// Code size 26 (0x1a)
.maxstack 1
.locals init (
[0] bool, // first bool variable
[1] bool // second bool variable
)
IL_0000: nop
IL_0001: ldarg.0 // load first bool
IL_0002: ldfld bool C::'value' // load value
IL_0007: stloc.0 // store field in first bool
// sequence point: hidden
IL_0008: ldloc.0 // load first bool
IL_0009: brfalse.s IL_000d // branch if false
IL_000b: nop // debug garbage
IL_000c: nop // debug garbage
IL_000d: ldarg.0 // load first bool
IL_000e: ldfld bool C::'value' // load value
IL_0013: stloc.1 // store value in second bool
// sequence point: hidden
IL_0014: ldloc.1 // load second bool
IL_0015: brfalse.s IL_0019 // branch if false
IL_0017: nop
IL_0018: nop
IL_0019: ret
} // end of method C::M
在调试模式下编译,但这并不重要。关键是,两个if
语句完全相同。发布版本要“更好”:
您的代码包含一个输入错误,在上,相同的错误发生在第行 你没有使用正确的秒表。复制/粘贴时要小心 使用正确的方法,您将得到几乎完全相同的执行时间,以证明FCin的答案 见结果:
我希望您是在发布模式下编译,并且在没有附加调试器的情况下执行(直接从visual studio中的命令行或shift-F5)。微基准是非常复杂的。啊,它是沙盒,而不是Sanbbox。那就把它与
真的
或进行比较吧!变量
而不是与false
比较?这将是我个人在处理bool
时的偏好。使用(启动时不附带调试器,发布,从控制台)@xanatos这是没有调试的,刚刚启动了exe。谢谢你向我指出这一点…@JordyvanEijk我会这样做的
// Fields
.field private bool 'value'
// Methods
.method public hidebysig
instance void M () cil managed
{
// Method begins at RVA 0x2050
// Code size 26 (0x1a)
.maxstack 1
.locals init (
[0] bool, // first bool variable
[1] bool // second bool variable
)
IL_0000: nop
IL_0001: ldarg.0 // load first bool
IL_0002: ldfld bool C::'value' // load value
IL_0007: stloc.0 // store field in first bool
// sequence point: hidden
IL_0008: ldloc.0 // load first bool
IL_0009: brfalse.s IL_000d // branch if false
IL_000b: nop // debug garbage
IL_000c: nop // debug garbage
IL_000d: ldarg.0 // load first bool
IL_000e: ldfld bool C::'value' // load value
IL_0013: stloc.1 // store value in second bool
// sequence point: hidden
IL_0014: ldloc.1 // load second bool
IL_0015: brfalse.s IL_0019 // branch if false
IL_0017: nop
IL_0018: nop
IL_0019: ret
} // end of method C::M
.field private static bool 'value'
.method private hidebysig static void
Main(
string[] args
) cil managed
{
.entrypoint
.maxstack 8
// [18 13 - 18 31]
IL_0000: ldsfld bool ConsoleA.Program::'value' // Load value
IL_0005: brfalse.s IL_000d // Branch if false
// USED ONLY SO COMPILER WONT COMPLETELY REMOVE IF STATEMENTS
// [20 17 - 20 36]
IL_0007: ldnull
IL_0008: call void [mscorlib]System.GC::KeepAlive(object)
// [23 13 - 23 31]
IL_000d: ldsfld bool ConsoleA.Program::'value' // Load value
IL_0012: brfalse.s IL_001a // Branch if false
// USED ONLY SO COMPILER WONT COMPLETELY REMOVE IF STATEMENTS
// [25 17 - 25 36]
IL_0014: ldnull
IL_0015: call void [mscorlib]System.GC::KeepAlive(object)
// [27 9 - 27 10]
IL_001a: ret
} // end of method Program::Main