-
Notifications
You must be signed in to change notification settings - Fork 272
/
If.cs
195 lines (167 loc) · 5.48 KB
/
If.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using MicroBenchmarks;
using System.Runtime.CompilerServices;
namespace IfStatements
{
[BenchmarkCategory(Categories.Runtime)]
public unsafe class IfStatements
{
private const int Iterations = 10000;
private const int MaxArgsPassed = 4; // Num of args in Consume
private static int[] inputs;
private static int s_seed;
static void InitRand() {
s_seed = 7774755;
}
static int Rand(ref int seed) {
s_seed = (s_seed * 77 + 13218009) % 3687091;
return seed;
}
public IfStatements()
{
inputs = new int[Iterations + MaxArgsPassed];
for (int i = 0; i < inputs.Length; i++) {
inputs[i] = Rand(ref s_seed) - 1;
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void Consume(int op1, int op2, int op3, int op4) {
return;
}
[Benchmark]
public void Single() {
for (int i = 0; i < Iterations; i++) {
SingleInner(inputs[i]);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void SingleInner(int op1) {
if (op1 % 2 == 0) {
op1 = 5;
}
Consume(op1, 0, 0, 0);
}
[Benchmark]
public void And() {
for (int i = 0; i < Iterations; i++) {
AndInner(inputs[i], inputs[i+1]);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void AndInner(int op1, int op2) {
if (op1 % 2 == 0 && op2 % 2 == 0) {
op1 = 5;
}
Consume(op1, op2, 0, 0);
}
[Benchmark]
public void AndAnd() {
for (int i = 0; i < Iterations; i++) {
AndAndInner(inputs[i], inputs[i+1], inputs[i+2]);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void AndAndInner(int op1, int op2, int op3) {
if (op1 % 2 == 0 && op2 % 2 == 0 && op3 % 2 == 0) {
op1 = 5;
}
Consume(op1, op2, op3, 0);
}
[Benchmark]
public void AndAndAnd() {
for (int i = 0; i < Iterations; i++) {
AndAndAndInner(inputs[i], inputs[i+1], inputs[i+2], inputs[i+3]);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void AndAndAndInner(int op1, int op2, int op3, int op4) {
if (op1 % 2 == 0 && op2 % 2 == 0 && op3 % 2 == 0 && op4 % 2 == 0) {
op1 = 5;
}
Consume(op1, op2, op3, op4);
}
[Benchmark]
public void Or() {
for (int i = 0; i < Iterations; i++) {
OrInner(inputs[i], inputs[i+1]);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void OrInner(int op1, int op2) {
if (op1 % 2 == 0 || op2 % 2 == 0) {
op1 = 5;
}
Consume(op1, op2, 0, 0);
}
[Benchmark]
public void OrOr() {
for (int i = 0; i < Iterations; i++) {
OrOrInner(inputs[i], inputs[i+1], inputs[i+2]);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void OrOrInner(int op1, int op2, int op3) {
if (op1 % 2 == 0 || op2 % 2 == 0 || op3 % 2 == 0) {
op1 = 5;
}
Consume(op1, op2, op3, 0);
}
[Benchmark]
public void AndOr() {
for (int i = 0; i < Iterations; i++) {
AndOrInner(inputs[i], inputs[i+1], inputs[i+2]);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void AndOrInner(int op1, int op2, int op3) {
if (op1 % 2 == 0 && op2 % 2 == 0 || op3 % 2 == 0) {
op1 = 5;
}
Consume(op1, op2, op3, 0);
}
[Benchmark]
public void SingleArray() {
for (int i = 0; i < Iterations; i++) {
SingleArrayInner(i);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void SingleArrayInner(int op1) {
if (inputs[op1] % 2 == 0) {
op1 = 5;
}
Consume(op1, 0, 0, 0);
}
[Benchmark]
public void AndArray() {
for (int i = 0; i < Iterations; i++) {
AndArrayInner(i, i+1);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void AndArrayInner(int op1, int op2) {
if (inputs[op1] % 2 == 0 && inputs[op2] % 2 == 0) {
op1 = 5;
}
Consume(op1, op2, 0, 0);
}
[Benchmark]
public void OrArray() {
for (int i = 0; i < Iterations; i++) {
OrArrayInner(i, i+1);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void OrArrayInner(int op1, int op2) {
if (inputs[op1] % 2 == 0 || inputs[op2] % 2 == 0) {
op1 = 5;
}
Consume(op1, op2, 0, 0);
}
}
}