diff --git a/src/benchmarks/micro/libraries/System.Collections/AddRemove/AddRemoveSteadyState.cs b/src/benchmarks/micro/libraries/System.Collections/AddRemove/AddRemoveSteadyState.cs index d16fd9cdbe0..791e0d30394 100644 --- a/src/benchmarks/micro/libraries/System.Collections/AddRemove/AddRemoveSteadyState.cs +++ b/src/benchmarks/micro/libraries/System.Collections/AddRemove/AddRemoveSteadyState.cs @@ -4,6 +4,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; +using System.Collections.Immutable; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Extensions; using MicroBenchmarks; @@ -20,6 +21,8 @@ public class Add_Remove_SteadyState // serialized producer/consumer throughpu private ConcurrentStack _concurrentStack; private Queue _queue; private Stack _stack; + private ImmutableQueue _immutableQueue; + private ImmutableStack _immutableStack; [Params(Utils.DefaultCollectionSize)] public int Count; @@ -73,5 +76,23 @@ public void Stack() T item = _stack.Pop(); _stack.Push(item); } + + [GlobalSetup(Target = nameof(ImmutableQueue))] + public void SetupImmutableQueue() => _immutableQueue = Immutable.ImmutableQueue.CreateRange(ValuesGenerator.ArrayOfUniqueValues(Count)); + + [Benchmark] + public void ImmutableQueue() + { + _immutableQueue = _immutableQueue.Dequeue(out T item).Enqueue(item); + } + + [GlobalSetup(Target = nameof(ImmutableStack))] + public void SetupImmutableStack() => _immutableStack = Immutable.ImmutableStack.CreateRange(ValuesGenerator.ArrayOfUniqueValues(Count)); + + [Benchmark] + public void ImmutableStack() + { + _immutableStack = _immutableStack.Pop(out T item).Push(item); + } } } diff --git a/src/benchmarks/micro/libraries/System.Collections/CreateAddAndClear.cs b/src/benchmarks/micro/libraries/System.Collections/CreateAddAndClear.cs index 49fd2c43784..247be837355 100644 --- a/src/benchmarks/micro/libraries/System.Collections/CreateAddAndClear.cs +++ b/src/benchmarks/micro/libraries/System.Collections/CreateAddAndClear.cs @@ -7,6 +7,7 @@ using MicroBenchmarks; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Collections.Immutable; using System.Runtime.CompilerServices; namespace System.Collections @@ -237,5 +238,93 @@ public ConcurrentBag ConcurrentBag() return concurrentBag; } #endif + + [Benchmark] + public ImmutableArray ImmutableArray() + { + ImmutableArray immutableArray = ImmutableArray.Empty; + foreach (T value in _uniqueValues) + { + immutableArray = immutableArray.Add(value); + } + return immutableArray.Clear(); + } + + [Benchmark] + public ImmutableList ImmutableList() + { + ImmutableList immutableList = ImmutableList.Empty; + foreach (T value in _uniqueValues) + { + immutableList = immutableList.Add(value); + } + return immutableList.Clear(); + } + + [Benchmark] + public ImmutableDictionary ImmutableDictionary() + { + ImmutableDictionary immutableDictionary = ImmutableDictionary.Empty; + foreach (T value in _uniqueValues) + { + immutableDictionary = immutableDictionary.Add(value, value); + } + return immutableDictionary.Clear(); + } + + [Benchmark] + public ImmutableHashSet ImmutableHashSet() + { + ImmutableHashSet immutableHashSet = ImmutableHashSet.Empty; + foreach (T value in _uniqueValues) + { + immutableHashSet = immutableHashSet.Add(value); + } + return immutableHashSet.Clear(); + } + + [Benchmark] + public ImmutableSortedDictionary ImmutableSortedDictionary() + { + ImmutableSortedDictionary immutableSortedDictionary = ImmutableSortedDictionary.Empty; + foreach (T value in _uniqueValues) + { + immutableSortedDictionary = immutableSortedDictionary.Add(value, value); + } + return immutableSortedDictionary.Clear(); + } + + [Benchmark] + public ImmutableSortedSet ImmutableSortedSet() + { + ImmutableSortedSet immutableSortedSet = ImmutableSortedSet.Empty; + foreach (T value in _uniqueValues) + { + immutableSortedSet = immutableSortedSet.Add(value); + } + return immutableSortedSet.Clear(); + } + + [Benchmark] + public ImmutableStack ImmutableStack() + { + ImmutableStack immutableStack = ImmutableStack.Empty; + foreach (T value in _uniqueValues) + { + immutableStack = immutableStack.Push(value); + } + return immutableStack.Clear(); + } + + [Benchmark] + public ImmutableQueue ImmutableQueue() + { + ImmutableQueue immutableQueue = ImmutableQueue.Empty; + foreach (T value in _uniqueValues) + { + immutableQueue = immutableQueue.Enqueue(value); + } + return immutableQueue.Clear(); + } } } \ No newline at end of file