-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-ForLoopBenchmarks.cs
More file actions
46 lines (40 loc) · 1.07 KB
/
Copy path1-ForLoopBenchmarks.cs
File metadata and controls
46 lines (40 loc) · 1.07 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
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
namespace DotnetPerf
{
// [SimpleJob(runtimeMoniker: RuntimeMoniker.Net461, baseline: true)]
// [SimpleJob(runtimeMoniker: RuntimeMoniker.CoreRt30, baseline: false)]
public class ForLoopBenchmarks
{
[Params(1000, 100000, 10000000)]
public int N;
private IList<int> data;
[GlobalSetup]
public void GlobalSetup()
{
data = Enumerable.Repeat(0, N).ToList();
}
[Benchmark(Baseline = true)]
public int ForLoop()
{
int res = 0;
for (int i = 0; i < N; i++)
res += data[i];
return res;
}
[Benchmark]
public int ForEachLoop()
{
int res = 0;
foreach (var i in data)
res += i;
return res;
}
[GlobalCleanup]
public void GlobalCleanup()
{
// Disposing logic
}
}
}