Skip to content

Instantly share code, notes, and snippets.

@Aimeast
Last active February 21, 2017 17:04
Show Gist options
  • Select an option

  • Save Aimeast/6752695 to your computer and use it in GitHub Desktop.

Select an option

Save Aimeast/6752695 to your computer and use it in GitHub Desktop.

Revisions

  1. Aimeast revised this gist Sep 29, 2013. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions Inlining.cs
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    /*
    * 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;
  2. Aimeast revised this gist Sep 29, 2013. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion Inlining.cs
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    /*
    * Following code for test an method will be inlined by JIT or not
    * Tested on Framework 4.5, Win8 x64
    */

    using System;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    @@ -10,7 +15,7 @@ static void Main(string[] args)
    Console.WriteLine(Fb(1)); // Fb
    Console.WriteLine(Fc(1)); // Main
    Console.WriteLine(Fd(1)); // Main
    Console.WriteLine(Fe(1)); // Fe
    Console.WriteLine(Fe(1)); // Fe if Platform target is x86, Main if x64
    Console.WriteLine(Ff(1)); // Ff
    }

  3. Aimeast created this gist Sep 29, 2013.
    61 changes: 61 additions & 0 deletions Inlining.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    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
    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;
    }
    }