비교를 위한 소스




Main 메소드


.method private hidebysig static 
    void Main (
        string[] args
    ) cil managed 
{
    // Method begins at RVA 0x2058
    // Code size 27 (0x1b)
    .maxstack 1
    .entrypoint
    .locals init (
        [0] string
    )

    // string str = "Hello world!";
    IL_0000: ldstr "Hello world!"
    IL_0005: stloc.0
    // Func(str);
    IL_0006: ldloc.0
    IL_0007: call void Program::Func(string)
    // FuncRef(ref str);
    IL_000c: ldloca.s 0
    IL_000e: call void Program::FuncRef(string&)
    // Console.ReadKey(true);
    IL_0013: ldc.i4.1
    IL_0014: call valuetype [mscorlib]System.ConsoleKeyInfo [mscorlib]System.Console::ReadKey(bool)
    IL_0019: pop
    // (no C# code)
    IL_001a: ret
} // end of method Program::Main


여기서 ldloc.0은 0번 로컬변수를 스택에 넣는 명령어고 ldloca.s 0은 0번 로컬변수의 주소를 스택에 넣는 명령어


.method private hidebysig static 
    void Func (
        string str
    ) cil managed 
{
    // Method begins at RVA 0x207f
    // Code size 14 (0xe)
    .maxstack 8

    // Console.WriteLine(str);
    IL_0000: ldarg.0
    IL_0001: call void [mscorlib]System.Console::WriteLine(string)
    // str = "";
    IL_0006: ldstr ""
    IL_000b: starg.s str
    // (no C# code)
    IL_000d: ret
} // end of method Program::Func


.method private hidebysig static 
    void FuncRef (
        string& str
    ) cil managed 
{
    // Method begins at RVA 0x208e
    // Code size 15 (0xf)
    .maxstack 8

    // Console.WriteLine(str);
    IL_0000: ldarg.0
    IL_0001: ldind.ref
    IL_0002: call void [mscorlib]System.Console::WriteLine(string)
    // str = "";
    IL_0007: ldarg.0
    IL_0008: ldstr ""
    IL_000d: stind.ref
    // (no C# code)
    IL_000e: ret
} // end of method Program::FuncRef


Func이랑 FuncRef의 함수 본문을보면 같은 동작인데도 명령어가 더 늘었지? 그럼 뭐가 더 빠를까