-
Notifications
You must be signed in to change notification settings - Fork 272
/
DelegatePerf.cs
131 lines (110 loc) · 4.88 KB
/
DelegatePerf.cs
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
namespace PerfLabTests
{
public delegate long DelegateLong(Object obj, long x, long y);
public delegate void MultiDelegate(Object obj, long x, long y);
[BenchmarkCategory(Categories.Runtime, Categories.Perflab)]
public class DelegatePerf
{
public static int InnerIterationCount200000 = 200000; // do not change the value and keep it public static NOT-readonly, ported "as is" from CoreCLR repo
public static int InnerIterationCount1000 = 1000; // do not change the value and keep it public static NOT-readonly, ported "as is" from CoreCLR repo
public static int InnerIterationCount10000 = 10000; // do not change the value and keep it public static NOT-readonly, ported "as is" from CoreCLR repo
DelegateLong dlField;
Object objField;
MultiDelegate md1Field, md2Field, md3Field, md4Field, md5Field, md6Field, md7Field, md8Field, md9Field, md10Field;
MultiDelegate md100Field, md1000Field;
[GlobalSetup(Target = nameof(DelegateInvoke))]
public void SetupDelegateInvoke()
{
dlField = new DelegateLong(this.Invocable1);
objField = new Object();
}
[Benchmark]
public long DelegateInvoke()
{
DelegateLong dl = dlField;
Object obj = objField;
long ret = 0;
for (int i = 0; i < InnerIterationCount200000; i++)
ret = dl(obj, 100, 100);
return ret;
}
[GlobalSetup(Target = nameof(MulticastDelegateCombineInvoke))]
public void SetupMulticastDelegateCombineInvoke()
{
md1Field = new MultiDelegate(this.Invocable2);
md2Field = new MultiDelegate(this.Invocable2);
md3Field = new MultiDelegate(this.Invocable2);
md4Field = new MultiDelegate(this.Invocable2);
md5Field = new MultiDelegate(this.Invocable2);
md6Field = new MultiDelegate(this.Invocable2);
md7Field = new MultiDelegate(this.Invocable2);
md8Field = new MultiDelegate(this.Invocable2);
md9Field = new MultiDelegate(this.Invocable2);
md10Field = new MultiDelegate(this.Invocable2);
}
[Benchmark]
public MultiDelegate MulticastDelegateCombineInvoke()
{
MultiDelegate md1 = md1Field;
MultiDelegate md2 = md2Field;
MultiDelegate md3 = md3Field;
MultiDelegate md4 = md4Field;
MultiDelegate md5 = md5Field;
MultiDelegate md6 = md6Field;
MultiDelegate md7 = md7Field;
MultiDelegate md8 = md8Field;
MultiDelegate md9 = md9Field;
MultiDelegate md10 = md10Field;
MultiDelegate md = null;
for (int i = 0; i < InnerIterationCount1000; i++)
{
md = (MultiDelegate)Delegate.Combine(md1, md);
md = (MultiDelegate)Delegate.Combine(md2, md);
md = (MultiDelegate)Delegate.Combine(md3, md);
md = (MultiDelegate)Delegate.Combine(md4, md);
md = (MultiDelegate)Delegate.Combine(md5, md);
md = (MultiDelegate)Delegate.Combine(md6, md);
md = (MultiDelegate)Delegate.Combine(md7, md);
md = (MultiDelegate)Delegate.Combine(md8, md);
md = (MultiDelegate)Delegate.Combine(md9, md);
md = (MultiDelegate)Delegate.Combine(md10, md);
}
return md;
}
[GlobalSetup(Target = nameof(MulticastDelegateInvoke))]
public void SetupMulticastDelegateInvoke()
{
objField = new Object();
md100Field = null;
md1000Field = null;
for (long i = 0; i < 100; i++)
md100Field = (MultiDelegate)Delegate.Combine(new MultiDelegate(this.Invocable2), md100Field);
for (long i = 0; i < 1000; i++)
md1000Field = (MultiDelegate)Delegate.Combine(new MultiDelegate(this.Invocable2), md1000Field);
}
[Benchmark]
[Arguments(100)]
[Arguments(1000)]
public void MulticastDelegateInvoke(int length)
{
MultiDelegate md = length == 100 ? md100Field : md1000Field;
Object obj = objField;
for (int i = 0; i < InnerIterationCount10000; i++)
md(obj, 100, 100);
}
internal virtual long Invocable1(Object obj, long x, long y)
{
long i = x + y;
return x;
}
internal virtual void Invocable2(Object obj, long x, long y)
{
long i = x + y;
}
}
}