-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathProgram.cs
More file actions
85 lines (70 loc) · 1.68 KB
/
Copy pathProgram.cs
File metadata and controls
85 lines (70 loc) · 1.68 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
// Made by Benjamin Abt - https://github.com/BenjaminAbt
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<Benchmark>();
[MemoryDiagnoser]
[SimpleJob(RuntimeMoniker.Net70)] // PGO enabled by default
[SimpleJob(RuntimeMoniker.Net80)]
[SimpleJob(RuntimeMoniker.Net90, baseline: true)]
[HideColumns(Column.Job)]
public class Benchmark
{
private IEnumerable<int> _collection;
private List<int> _list;
[GlobalSetup]
public void GlobalSetup()
{
const int to = 1000;
_collection = Enumerable.Range(0, to);
_list = Enumerable.Range(0, to).ToList();
}
[Benchmark]
public int IEnumerable()
{
return RunIEnumerable(_collection);
}
[Benchmark]
public int IEnumerable_ToList()
{
return RunList(_collection.ToList());
}
[Benchmark]
public int List()
{
return RunList(_list);
}
[Benchmark]
public int List_IEnumerable()
{
IEnumerable<int> c = _list;
return RunIEnumerable(c);
}
[Benchmark]
public int List_IEnumerable_ToList()
{
IEnumerable<int> c = _list;
return RunList(c.ToList());
}
private static int RunIEnumerable(IEnumerable<int> enumerable)
{
int sum = 0;
foreach (int item in enumerable)
{
sum = sum + item;
}
return sum;
}
private static int RunList(List<int> list)
{
int sum = 0;
foreach (int item in list)
{
sum = sum + item;
}
return sum;
}
}