-
Notifications
You must be signed in to change notification settings - Fork 272
/
CtorFromCollection.cs
111 lines (81 loc) · 4.79 KB
/
CtorFromCollection.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
// 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.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;
namespace System.Collections
{
[BenchmarkCategory(Categories.Libraries, Categories.Collections, Categories.GenericCollections)]
[GenericTypeArguments(typeof(int))] // value type
[GenericTypeArguments(typeof(string))] // reference type
public class CtorFromCollection<T>
{
private ICollection<T> _collection;
private IDictionary<T, T> _dictionary;
private SortedDictionary<T, T> _sortedDictionary;
[Params(Utils.DefaultCollectionSize)]
public int Size;
[GlobalSetup(Targets = new[] { nameof(List), nameof(LinkedList), nameof(HashSet), nameof(Queue), nameof(Stack), nameof(SortedSet), nameof(ConcurrentQueue), nameof(ConcurrentStack),
nameof(ConcurrentBag), nameof(ImmutableArray), nameof(ImmutableHashSet), nameof(ImmutableList), nameof(ImmutableQueue), nameof(ImmutableStack), nameof(ImmutableSortedSet),
nameof(FrozenSet)})]
public void SetupCollection() => _collection = ValuesGenerator.ArrayOfUniqueValues<T>(Size);
[GlobalSetup(Targets = new[] { nameof(Dictionary), nameof(SortedList), nameof(SortedDictionary), nameof(ConcurrentDictionary), nameof(ImmutableDictionary), nameof(ImmutableSortedDictionary), nameof(FrozenDictionaryOptimized) })]
public void SetupDictionary() => _dictionary = ValuesGenerator.Dictionary<T, T>(Size);
[GlobalSetup(Targets = new[] { nameof(SortedDictionaryDeepCopy) })]
public void SetupSortedDictionary() => _sortedDictionary = new SortedDictionary<T, T>(ValuesGenerator.Dictionary<T, T>(Size));
[Benchmark]
public List<T> List() => new List<T>(_collection);
[Benchmark]
public LinkedList<T> LinkedList() => new LinkedList<T>(_collection);
[Benchmark]
public HashSet<T> HashSet() => new HashSet<T>(_collection);
[Benchmark]
public Dictionary<T, T> Dictionary() => new Dictionary<T, T>(_dictionary);
[Benchmark]
public Queue<T> Queue() => new Queue<T>(_collection);
[Benchmark]
public Stack<T> Stack() => new Stack<T>(_collection);
[Benchmark]
public SortedList<T, T> SortedList() => new SortedList<T, T>(_dictionary);
[Benchmark]
public SortedSet<T> SortedSet() => new SortedSet<T>(_collection);
[Benchmark]
public SortedDictionary<T, T> SortedDictionary() => new SortedDictionary<T, T>(_dictionary);
[Benchmark]
public SortedDictionary<T, T> SortedDictionaryDeepCopy() => new SortedDictionary<T, T>(_sortedDictionary);
[Benchmark]
public ConcurrentDictionary<T, T> ConcurrentDictionary() => new ConcurrentDictionary<T, T>(_dictionary);
[Benchmark]
public ConcurrentQueue<T> ConcurrentQueue() => new ConcurrentQueue<T>(_collection);
[Benchmark]
public ConcurrentStack<T> ConcurrentStack() => new ConcurrentStack<T>(_collection);
[Benchmark]
public ConcurrentBag<T> ConcurrentBag() => new ConcurrentBag<T>(_collection);
[Benchmark]
public ImmutableArray<T> ImmutableArray() => Immutable.ImmutableArray.CreateRange<T>(_collection);
[Benchmark]
public ImmutableDictionary<T, T> ImmutableDictionary() => Immutable.ImmutableDictionary.CreateRange<T, T>(_dictionary);
[Benchmark]
public ImmutableHashSet<T> ImmutableHashSet() => Immutable.ImmutableHashSet.CreateRange<T>(_collection);
[Benchmark]
public ImmutableList<T> ImmutableList() => Immutable.ImmutableList.CreateRange<T>(_collection);
[Benchmark]
public ImmutableQueue<T> ImmutableQueue() => Immutable.ImmutableQueue.CreateRange<T>(_collection);
[Benchmark]
public ImmutableStack<T> ImmutableStack() => Immutable.ImmutableStack.CreateRange<T>(_collection);
[Benchmark]
public ImmutableSortedDictionary<T, T> ImmutableSortedDictionary() => Immutable.ImmutableSortedDictionary.CreateRange<T, T>(_dictionary);
[Benchmark]
public ImmutableSortedSet<T> ImmutableSortedSet() => Immutable.ImmutableSortedSet.CreateRange<T>(_collection);
[Benchmark(Description = "FrozenDictionary")]
public FrozenDictionary<T, T> FrozenDictionaryOptimized() // we kept the old name on purpose to avoid loosing historical data
=> _dictionary.ToFrozenDictionary();
[Benchmark]
public FrozenSet<T> FrozenSet() => _collection.ToFrozenSet();
}
}