-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathProgram.cs
76 lines (64 loc) · 1.47 KB
/
Program.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
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<ListBenchmarks>();
[MemoryDiagnoser]
public class ListBenchmarks
{
[Params(1000, 20_0000)] public int Items { get; set; }
[Benchmark(Baseline = true)]
public List<int> List()
{
var list = new List<int>();
for (var i = 0; i < Items; i++)
{
list.Add(i);
}
return list;
}
[Benchmark]
public ChunkedList<int> ChunkedList()
{
var list = new ChunkedList<int>();
for (var i = 0; i < Items; i++)
{
list.Add(i);
}
return list;
}
}
public class ForBenchmark
{
private readonly List<int> _list = new List<int>();
private readonly ChunkedList<int> _chunkedList = new ChunkedList<int>();
[GlobalSetup]
public void Setup()
{
_list.AddRange(Enumerable.Range(0, 20_000));
foreach (var item in _list)
{
_chunkedList.Add(item);
}
}
[Benchmark(Baseline = true)]
public int ForList()
{
var sum = 0;
for (var i = 0; i < _list.Count; i++)
{
sum += _list[i];
}
return sum;
}
[Benchmark]
public int ForChunkedList()
{
var sum = 0;
for (var i = 0; i < _chunkedList.Count; i++)
{
sum += _chunkedList[i];
}
return sum;
}
}