在GOTO
代码中是否存在c#
语句的性能影响,而不是使用loop
或多个单独的用户定义的functions
。
我不确定但是GOTO
语句可能会出现内存跳转并且会影响性能。
不,它没有。
如果不知道替代方案是什么,很难回答“它会影响性能”,但我会编制一个样本:
using System;
public class C {
public void M() {
var x = 7;
switch(x) // START SWITCH
{
case 1:
Console.WriteLine("Hello");
break;
case 2:
Console.WriteLine("World");
break;
default:
Console.WriteLine("Uh...");
break;
}
// END SWITCH
// START GOTO
if (x == 1)
{
goto Hello;
}
else if (x == 2)
{
goto World;
}
Console.WriteLine("Uh...");
goto End;
Hello:
Console.WriteLine("Hello");
goto End;
World:
Console.WriteLine("World");
End:
// END GOTO
Console.WriteLine("Done");
}
}
在发布模式下使用C#进行编译(使用sharplab.io默认值为2.9.0)会生成以下IL(即Java民谣的字节码):
.class private auto ansi '<Module>'
{
} // end of class <Module>
.class public auto ansi beforefieldinit C
extends [mscorlib]System.Object
{
// Methods
.method public hidebysig
instance void M () cil managed
{
// Method begins at RVA 0x2050
// Code size 99 (0x63)
.maxstack 2
.locals init (
[0] int32
)
IL_0000: ldc.i4.7
IL_0001: stloc.0
// START SWITCH
IL_0002: ldloc.0
IL_0003: ldc.i4.1
IL_0004: beq.s IL_000c
IL_0006: ldloc.0
IL_0007: ldc.i4.2
IL_0008: beq.s IL_0018
IL_000a: br.s IL_0024
IL_000c: ldstr "Hello"
IL_0011: call void [mscorlib]System.Console::WriteLine(string)
IL_0016: br.s IL_002e
IL_0018: ldstr "World"
IL_001d: call void [mscorlib]System.Console::WriteLine(string)
IL_0022: br.s IL_002e
IL_0024: ldstr "Uh..."
IL_0029: call void [mscorlib]System.Console::WriteLine(string)
// END SWITCH
// START GOTO
IL_002e: ldloc.0
IL_002f: ldc.i4.1
IL_0030: beq.s IL_0042
IL_0032: ldloc.0
IL_0033: ldc.i4.2
IL_0034: beq.s IL_004e
IL_0036: ldstr "Uh..."
IL_003b: call void [mscorlib]System.Console::WriteLine(string)
IL_0040: br.s IL_0058
IL_0042: ldstr "Hello"
IL_0047: call void [mscorlib]System.Console::WriteLine(string)
IL_004c: br.s IL_0058
IL_004e: ldstr "World"
IL_0053: call void [mscorlib]System.Console::WriteLine(string)
// END GOTO
IL_0058: ldstr "Done"
IL_005d: call void [mscorlib]System.Console::WriteLine(string)
IL_0062: ret
} // end of method C::M
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// Method begins at RVA 0x20bf
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method C::.ctor
} // end of class C
开关形式有3个beq
和1个br.s
,goto形式有2个beq
和2个br.s
,除此之外它们是相同的。 br.s
的成本可能小于或等于beq
的成本,因此goto方法的成本不高于切换方法的成本。
最后,使用goto是个坏主意。如果你想争论这个事实,请在另一个问题上这样做:What is wrong with using goto?