-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathConcurrentQueueMethodImplBenchmarks.cs
More file actions
84 lines (77 loc) · 2.5 KB
/
ConcurrentQueueMethodImplBenchmarks.cs
File metadata and controls
84 lines (77 loc) · 2.5 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
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Platform.Collections.Benchmarks
{
/// <summary>
/// Benchmark to test the impact of MethodImpl(MethodImplOptions.AggressiveInlining)
/// on ConcurrentQueue extension methods performance.
/// </summary>
[SimpleJob(invocationCount: 1, warmupCount: 3, targetCount: 5)]
[MemoryDiagnoser]
public class ConcurrentQueueMethodImplBenchmarks
{
[Params(10, 100, 1000)]
public int QueueSize { get; set; }
/// <summary>
/// Test DequeueAll with MethodImpl(MethodImplOptions.AggressiveInlining)
/// </summary>
[Benchmark(Baseline = true)]
public int DequeueAllWithMethodImpl()
{
var queue = new ConcurrentQueue<int>();
for (int i = 0; i < QueueSize; i++)
{
queue.Enqueue(i);
}
int count = 0;
foreach (var item in DequeueAllWithInlining(queue))
{
count++;
}
return count;
}
/// <summary>
/// Test DequeueAll without MethodImpl attribute
/// </summary>
[Benchmark]
public int DequeueAllWithoutMethodImpl()
{
var queue = new ConcurrentQueue<int>();
for (int i = 0; i < QueueSize; i++)
{
queue.Enqueue(i);
}
int count = 0;
foreach (var item in DequeueAllWithoutInlining(queue))
{
count++;
}
return count;
}
/// <summary>
/// Version WITH MethodImpl(MethodImplOptions.AggressiveInlining)
/// Copy of the original implementation from ConcurrentQueueExtensions
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static IEnumerable<T> DequeueAllWithInlining<T>(ConcurrentQueue<T> queue)
{
while (queue.TryDequeue(out T item))
{
yield return item;
}
}
/// <summary>
/// Version WITHOUT MethodImpl attribute for comparison
/// </summary>
private static IEnumerable<T> DequeueAllWithoutInlining<T>(ConcurrentQueue<T> queue)
{
while (queue.TryDequeue(out T item))
{
yield return item;
}
}
}
}