-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathCount.cs
58 lines (46 loc) · 2.04 KB
/
Count.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
// 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.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;
namespace System.Collections.Concurrent
{
[BenchmarkCategory(Categories.Libraries, Categories.Collections, Categories.GenericCollections)]
[GenericTypeArguments(typeof(int))] // value type
[GenericTypeArguments(typeof(string))] // reference type
public class Count<T>
{
private ConcurrentDictionary<T, T> _dictionary;
private ConcurrentQueue<T> _queue;
private ConcurrentStack<T> _stack;
private ConcurrentBag<T> _bag;
[Params(Utils.DefaultCollectionSize)]
public int Size;
[GlobalSetup(Target = nameof(Dictionary))]
public void SetupDictionary() => _dictionary = new ConcurrentDictionary<T, T>(ValuesGenerator.ArrayOfUniqueValues<T>(Size).ToDictionary(v => v, v => v));
[Benchmark]
public int Dictionary() => _dictionary.Count;
[GlobalSetup(Targets = new[] { nameof(Queue), nameof(Queue_EnqueueCountDequeue) })]
public void SetupQueue() => _queue = new ConcurrentQueue<T>(ValuesGenerator.ArrayOfUniqueValues<T>(Size));
[Benchmark]
public int Queue() => _queue.Count;
[Benchmark]
public int Queue_EnqueueCountDequeue()
{
_queue.Enqueue(default);
int c = _queue.Count;
_queue.TryDequeue(out _);
return c;
}
[GlobalSetup(Target = nameof(Stack))]
public void SetupStack() => _stack = new ConcurrentStack<T>(ValuesGenerator.ArrayOfUniqueValues<T>(Size));
[Benchmark]
public int Stack() => _stack.Count;
[GlobalSetup(Target = nameof(Bag))]
public void SetupBag() => _bag = new ConcurrentBag<T>(ValuesGenerator.ArrayOfUniqueValues<T>(Size));
[Benchmark]
public int Bag() => _bag.Count;
}
}