简体   繁体   English

goto语句会影响C#的性能吗

[英]does goto statement impact performance in c#

Is there performance impact of GOTO statement in c# code, instead of using loop or multiple separate user defined functions . c#代码中使用GOTO语句是否会对性能产生影响,而不是使用loop或多个单独的用户定义functions

I am not sure but memory jump may occur with GOTO statement and can impact performance. 我不确定,但是GOTO语句可能会发生内存跳跃,并会影响性能。

No, it doesn't. 不,不是。

It's hard to answer "does it impact peformance" without knowing what the alternative is but I'll make up a sample: 在不知道替代方案是什么的情况下,很难回答“是否会影响性能”,但我将组成一个示例:

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");
    }
}

Compiling this with C# in release mode (using sharplab.io default 2.9.0) yields the following IL (ie bytecode for you Java folk): 在发布模式下使用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

The switch form has 3 beq and 1 br.s and the goto form has 2 beq and 2 br.s , other than that they are identical. 交换形式具有3 beq和1 br.s ,而goto形式具有2 beq和2 br.s ,除了它们相同。 The cost of br.s is presumably less than or equal to the cost of beq so the cost of the goto approach is not greater than the cost of the switch approach. br.s的成本大概小于或等于beq的成本,因此goto方法的成本不大于switch方法的成本。

Finally, using goto is a bad idea. 最后,使用goto是一个坏主意。 If you want to argue about that fact please do so on a different question like this one: What is wrong with using goto? 如果您想就这一事实争论不休,请针对这样一个不同的问题: 使用goto有什么问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM