Created
October 29, 2019 13:52
-
-
Save danielwertheim/23ca65dc73cf6c4f24e6241c340b49b0 to your computer and use it in GitHub Desktop.
ConcurrentQueue vs Queueu + lock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Concurrent; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Running; | |
namespace ConsoleApp2 | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var summary1 = BenchmarkRunner.Run<Bench>(); | |
} | |
} | |
[MemoryDiagnoser] | |
public class Bench | |
{ | |
private const int N1 = 10000; | |
private const int N2 = 100000; | |
private const int N3 = 1000000; | |
private Task Simple(int n) | |
{ | |
var sync = new object(); | |
var q = new Queue<int>(); | |
var producer = Task.Run(() => | |
{ | |
for (var c = 0; c < n; c++) | |
{ | |
lock (sync) | |
q.Enqueue(c); | |
} | |
}); | |
var consumer = Task.Run(() => | |
{ | |
var c = 0; | |
while(c < n) | |
{ | |
lock (sync) | |
{ | |
if (q.TryDequeue(out _)) | |
c++; | |
} | |
} | |
}); | |
return Task.WhenAll(producer, consumer); | |
} | |
private Task Complex(int n) | |
{ | |
var q = new ConcurrentQueue<int>(); | |
var producer = Task.Run(() => | |
{ | |
for (var c = 0; c < n; c++) | |
{ | |
q.Enqueue(c); | |
} | |
}); | |
var consumer = Task.Run(() => | |
{ | |
var c = 0; | |
while(c < n) | |
{ | |
if (q.TryDequeue(out _)) | |
c++; | |
} | |
}); | |
return Task.WhenAll(producer, consumer); | |
} | |
[Benchmark] | |
public async Task SimpleN1() => await Simple(N1); | |
[Benchmark] | |
public async Task SimpleN2() => await Simple(N2); | |
[Benchmark] | |
public async Task SimpleN3() => await Simple(N3); | |
[Benchmark] | |
public async Task ComplexN1() => await Complex(N1); | |
[Benchmark] | |
public async Task ComplexN2() => await Complex(N2); | |
[Benchmark] | |
public async Task ComplexN3() => await Complex(N3); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Method | Mean | Error | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated | | |
|---------- |------------:|------------:|------------:|--------:|--------:|--------:|----------:| | |
| SimpleN1 | 820.9 us | 16.40 us | 18.89 us | 10.7422 | - | - | 43.1 KB | | |
| SimpleN2 | 7,119.4 us | 150.62 us | 211.15 us | 54.6875 | 54.6875 | 54.6875 | 290.36 KB | | |
| SimpleN3 | 71,158.2 us | 2,527.72 us | 7,373.47 us | - | - | - | 356.98 KB | | |
| ComplexN1 | 272.9 us | 5.45 us | 9.82 us | 3.9063 | 0.4883 | - | 16.8 KB | | |
| ComplexN2 | 2,083.9 us | 41.19 us | 95.47 us | 3.9063 | - | - | 27.99 KB | | |
| ComplexN3 | 17,201.3 us | 340.41 us | 725.43 us | - | - | - | 147.69 KB | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment