csharp 测试C#内联方法(框架4.5)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 测试C#内联方法(框架4.5)相关的知识,希望对你有一定的参考价值。

/*
 * Following code for test an method will be inlined by JIT or not
 * Tested on Framework 4.5, Win8 x64
 * 
 * http://blogs.msdn.com/b/ericgu/archive/2004/01/29/64717.aspx
 * http://blogs.microsoft.co.il/blogs/sasha/archive/2012/01/20/aggressive-inlining-in-the-clr-4-5-jit.aspx
 */

using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Fa(1)); // Main
        Console.WriteLine(Fb(1)); // Fb
        Console.WriteLine(Fc(1)); // Main
        Console.WriteLine(Fd(1)); // Main
        Console.WriteLine(Fe(1)); // Fe if Platform target is x86, Main if x64
        Console.WriteLine(Ff(1)); // Ff
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    static string Fa(int a)
    {
        var s = a.ToString();
        var b = uint.Parse(s);
        var c = (a + b) / 2;
        var ss = c.ToString();
        var d = int.Parse(ss);
        return new StackFrame().GetMethod().Name;
    }

    static string Fb(int a)
    {
        var s = a.ToString();
        var b = uint.Parse(s);
        var c = (a + b) / 2;
        var ss = c.ToString();
        var d = int.Parse(ss);
        return new StackFrame().GetMethod().Name;
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    static string Fc(int a)
    {
        return new StackFrame().GetMethod().Name;
    }

    static string Fd(int a)
    {
        return new StackFrame().GetMethod().Name;
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    static string Fe(int a)
    {
        a += 1;
        return new StackFrame().GetMethod().Name;
    }

    static string Ff(int a)
    {
        a += 1;
        return new StackFrame().GetMethod().Name;
    }
}

以上是关于csharp 测试C#内联方法(框架4.5)的主要内容,如果未能解决你的问题,请参考以下文章

csharp [C#] [MVC]缩小内联脚本和样式

csharp 使用内联css / javascript为MVC C#缩小HTML作为ActionFilterAttribute

csharp 使用内联css / javascript为MVC C#缩小HTML作为ActionFilterAttribute

Linq 内联左联等

iframe内联框架

WindowsGUI自动化测试框架搭建(十五)-框架README.md设计