Timothy Schenk
9f3007b10e
All checks were successful
Build, Package and Push Images / preprocess (push) Successful in 2s
Build, Package and Push Images / build (push) Successful in 25s
Build, Package and Push Images / sbom-scan (push) Successful in 49s
Build, Package and Push Images / container-build (push) Successful in 2m11s
Build, Package and Push Images / sonarqube (push) Successful in 2m12s
Build, Package and Push Images / container-sbom-scan (push) Successful in 39s
84 lines
2.4 KiB
C#
84 lines
2.4 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Collections.Immutable;
|
|
using BenchmarkDotNet.Attributes;
|
|
using BenchmarkDotNet.Order;
|
|
|
|
namespace Benchmarks;
|
|
|
|
[Config(typeof(GenericConfig))]
|
|
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
|
|
public class DataCacheBenchmark
|
|
{
|
|
private ConcurrentDictionary<int, int> _concurrentDictionary;
|
|
private Dictionary<int, int> _dictionary;
|
|
private HashSet<int> _hashSet;
|
|
private ImmutableHashSet<int> _immutableHashSet;
|
|
[Params(1000, 100000, 1000000)] public int N;
|
|
|
|
[GlobalSetup]
|
|
public void Setup()
|
|
{
|
|
_hashSet = new HashSet<int>();
|
|
_dictionary = new Dictionary<int, int>();
|
|
_concurrentDictionary = new ConcurrentDictionary<int, int>();
|
|
_immutableHashSet = ImmutableHashSet<int>.Empty;
|
|
|
|
_hashSet.Clear();
|
|
_dictionary.Clear();
|
|
_concurrentDictionary.Clear();
|
|
_immutableHashSet = _immutableHashSet.Clear();
|
|
_hashSet.EnsureCapacity(N);
|
|
_dictionary.EnsureCapacity(N);
|
|
|
|
for (var i = 0; i < N; i++)
|
|
{
|
|
_immutableHashSet = _immutableHashSet.Add(i);
|
|
_hashSet.Add(i);
|
|
_dictionary.Add(i, i);
|
|
_concurrentDictionary.TryAdd(i, i);
|
|
}
|
|
}
|
|
|
|
[Benchmark]
|
|
public void HashSetAdd()
|
|
{
|
|
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => _hashSet.Add(i));
|
|
}
|
|
|
|
[Benchmark]
|
|
public void DictionaryAdd()
|
|
{
|
|
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => _dictionary.Add(N + i, i));
|
|
}
|
|
|
|
[Benchmark]
|
|
public void ConcurrentDictionaryAddOrUpdate()
|
|
{
|
|
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i =>
|
|
_concurrentDictionary.AddOrUpdate(N + i, i, (key, oldValue) => oldValue + i));
|
|
}
|
|
|
|
[Benchmark]
|
|
public void ImmutableHashSetLookup()
|
|
{
|
|
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => _immutableHashSet.Contains(i));
|
|
}
|
|
|
|
[Benchmark]
|
|
public void HashSetLookup()
|
|
{
|
|
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => _hashSet.Contains(i));
|
|
}
|
|
|
|
[Benchmark]
|
|
public void DictionaryLookup()
|
|
{
|
|
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => _dictionary.ContainsKey(i));
|
|
}
|
|
|
|
[Benchmark]
|
|
public void ConcurrentDictionaryLookup()
|
|
{
|
|
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => _concurrentDictionary.ContainsKey(i));
|
|
}
|
|
}
|