Skip to content

Instantly share code, notes, and snippets.

@laicasaane
Created April 19, 2022 08:44
Show Gist options
  • Save laicasaane/69cb45821a20bba4fd853826f46581f3 to your computer and use it in GitHub Desktop.
Save laicasaane/69cb45821a20bba4fd853826f46581f3 to your computer and use it in GitHub Desktop.
Comparing inlining method and no inlining method
using System.Diagnostics;
using System.Runtime.CompilerServices;
class Program
{
const int MAX = 10000000;
static void Main()
{
// ... Compile the methods.
Add_NoInlining(4, 5);
Add_Inlining(8, 9);
int sum = 0;
var s1 = Stopwatch.StartNew();
for (int i = 0; i < MAX; i++)
{
sum += Add_NoInlining(i, i-1);
}
s1.Stop();
var s2 = Stopwatch.StartNew();
for (int i = 0; i < MAX; i++)
{
sum += Add_Inlining(i, i-1);
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / MAX).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / MAX).ToString("0.00 ns"));
}
[MethodImpl(MethodImplOptions.NoInlining)]
static int Add_NoInlining(int x, int y)
{
return x + y;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int Add_Inlining(int x, int y)
{
return x + y;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment