我想问第一个例子是否比第二个例子慢一些。
示例1:针对,不安全,不安全,不安全等
for (var i = 0; i < ...; i++)
{
unsafe
{
// ...
}
}
示例2:不安全,针对
unsafe
{
for (var i = 0; i < ...; i++)
{
// ...
}
}
无处涉及此方面。
问题:
有人可以假设in the documentation代码块的位置不会影响性能吗?
它们产生完全相同的IL代码,所以没有性能差异
嵌套:unsafe
c#
See on SharpLab
IL
using System;
public class C {
public void M() {
var x = 0;
for (var i = 0; i < 100; i++)
{
unsafe { x+=i; }
}
}
}
环绕:.class private auto ansi '<Module>'
{
} // end of class <Module>
.class public auto ansi beforefieldinit C
extends [System.Private.CoreLib]System.Object
{
// Methods
.method public hidebysig
instance void M () cil managed
{
// Method begins at RVA 0x2050
// Code size 20 (0x14)
.maxstack 2
.locals init (
[0] int32,
[1] int32
)
IL_0000: ldc.i4.0
IL_0001: stloc.0
IL_0002: ldc.i4.0
IL_0003: stloc.1
// sequence point: hidden
IL_0004: br.s IL_000e
// loop start (head: IL_000e)
IL_0006: ldloc.0
IL_0007: ldloc.1
IL_0008: add
IL_0009: stloc.0
IL_000a: ldloc.1
IL_000b: ldc.i4.1
IL_000c: add
IL_000d: stloc.1
IL_000e: ldloc.1
IL_000f: ldc.i4.s 100
IL_0011: blt.s IL_0006
// end loop
IL_0013: ret
} // end of method C::M
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// Method begins at RVA 0x2070
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [System.Private.CoreLib]System.Object::.ctor()
IL_0006: ret
} // end of method C::.ctor
} // end of class C
c#
See on SharpLab
IL
using System;
public class C {
public void M() {
var x = 0;
unsafe {
for (var i = 0; i < 100; i++)
{
x+=i;
}
}
}
}