Skip to content

Commit 796c59a

Browse files
committed
Parallel inserts into queue vs bag
1 parent 3f37f6b commit 796c59a

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Collections.Concurrent;
2+
using System.Threading.Tasks;
3+
using BenchmarkDotNet.Attributes;
4+
using BenchmarkDotNet.Columns;
5+
using BenchmarkDotNet.Configs;
6+
using BenchmarkDotNet.Diagnosers;
7+
using BenchmarkDotNet.Exporters;
8+
using BenchmarkDotNet.Jobs;
9+
10+
namespace MicroBenchmarks.Linq
11+
{
12+
[Config(typeof(Config))]
13+
public class ConcurrentQueueVsConcurrentBag_Adding
14+
{
15+
private ConcurrentQueue<int> queue;
16+
private ConcurrentBag<int> bag;
17+
18+
private class Config : ManualConfig
19+
{
20+
public Config()
21+
{
22+
Add(MarkdownExporter.GitHub);
23+
Add(MemoryDiagnoser.Default);
24+
Add(StatisticColumn.AllStatistics);
25+
Add(Job.MediumRun);
26+
}
27+
}
28+
29+
[GlobalSetup]
30+
public void SetUp()
31+
{
32+
queue = new ConcurrentQueue<int>();
33+
bag = new ConcurrentBag<int>();
34+
}
35+
36+
[Benchmark(Baseline = true)]
37+
public void ConcurrentQueue_Enqueue()
38+
{
39+
Parallel.For(0, 1000, i => queue.Enqueue(i));
40+
}
41+
42+
[Benchmark]
43+
public void ConcurrentBag_Add()
44+
{
45+
Parallel.For(0, 1000, i => bag.Add(i));
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)