forked from drorgl/ForBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmallMethodsTest.cs
More file actions
95 lines (76 loc) · 2.53 KB
/
SmallMethodsTest.cs
File metadata and controls
95 lines (76 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace InlineMethodTest
{
class SmallMethodsTest
{
delegate int delegatetest(int a1, int s2, int a3);
public static void Execute()
{
int iterations = 5000000;
Stopwatch sw = Stopwatch.StartNew();
for (var i = 0; i < iterations; i++)
{
var res = MathTest(i, 1, 3);
}
Console.WriteLine("No Inline: {0}ms", sw.ElapsedMilliseconds);
sw = Stopwatch.StartNew();
for (var i = 0; i < iterations; i++)
{
var res = MathTestInline(i, 1, 3);
}
Console.WriteLine("Inline: {0}ms", sw.ElapsedMilliseconds);
sw = Stopwatch.StartNew();
for (var i = 0; i < iterations; i++)
{
var res = MathTestDefault(i, 1, 3);
}
Console.WriteLine("Default: {0}ms", sw.ElapsedMilliseconds);
sw = Stopwatch.StartNew();
for (var i = 0; i < iterations; i++)
{
var res = (i+1) * 3;
}
Console.WriteLine("Code: {0}ms", sw.ElapsedMilliseconds);
sw = Stopwatch.StartNew();
delegatetest delegatemthod = delegate(int a1, int a2, int a3)
{
return (a1 + a2) * a3;
};
for (var i = 0; i < iterations; i++)
{
var res = delegatemthod(i, 1, 3);
}
Console.WriteLine("Delegate: {0}ms", sw.ElapsedMilliseconds);
Func<int, int, int, int> lambdatest = (a1, a2, a3) =>
{
return (a1 + a2) * a3;
};
sw = Stopwatch.StartNew();
for (var i = 0; i < iterations; i++)
{
var res = lambdatest(i, 1, 3);
}
Console.WriteLine("Lambda: {0}ms", sw.ElapsedMilliseconds);
}
private static int MathTestDefault(int a1, int a2, int a3)
{
return (a1 + a2) * a3;
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static int MathTest(int a1, int a2, int a3)
{
return (a1 + a2) * a3;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int MathTestInline(int a1, int a2, int a3)
{
return (a1 + a2) * a3;
}
}
}