Generating assembly code from C# code?

后端 未结 4 1654
伪装坚强ぢ
伪装坚强ぢ 2020-12-19 06:41

Is there any way to generate assembly code from C# code? I know that it is possible with C code with GAS, but does anybody know if it\'s possible with C#?

4条回答
  •  死守一世寂寞
    2020-12-19 07:08

    You can use BenchmarkDotNet with the printAsm flag set to true.

    [DisassemblyDiagnoser(printAsm: true, printSource: true)] // !!! use the new diagnoser!!
    [RyuJitX64Job]
    public class Simple
    {
        int[] field = Enumerable.Range(0, 100).ToArray();
    
        [Benchmark]
        public int SumLocal()
        {
            var local = field; // we use local variable that points to the field
    
            int sum = 0;
            for (int i = 0; i < local.Length; i++)
                sum += local[i];
    
            return sum;
        }
    
        [Benchmark]
        public int SumField()
        {
            int sum = 0;
            for (int i = 0; i < field.Length; i++)
                sum += field[i];
    
            return sum;
        }
    }
    

    Which produces:

提交回复
热议问题