Skip to content

Commit

Permalink
Add CreateAddAndRemove benchmarks for immutable collections.
Browse files Browse the repository at this point in the history
  • Loading branch information
madelson committed Mar 10, 2022
1 parent ea63c33 commit ac3b84b
Showing 1 changed file with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using BenchmarkDotNet.Extensions;
using MicroBenchmarks;
using System.Collections.Generic;
using System.Collections.Immutable;

namespace System.Collections
{
Expand Down Expand Up @@ -157,5 +158,125 @@ public Queue<T> Queue()
}
return queue;
}

[Benchmark]
public ImmutableArray<T> ImmutableArray()
{
ImmutableArray<T> immutableArray = ImmutableArray<T>.Empty;
foreach (T uniqueKey in _keys)
{
immutableArray = immutableArray.Add(uniqueKey);
}
foreach (T uniqueKey in _keys)
{
immutableArray = immutableArray.Remove(uniqueKey);
}
return immutableArray;
}

[Benchmark]
public ImmutableList<T> ImmutableList()
{
ImmutableList<T> immutableList = ImmutableList<T>.Empty;
foreach (T uniqueKey in _keys)
{
immutableList = immutableList.Add(uniqueKey);
}
foreach (T uniqueKey in _keys)
{
immutableList = immutableList.Remove(uniqueKey);
}
return immutableList;
}

[Benchmark]
public ImmutableHashSet<T> ImmutableHashSet()
{
ImmutableHashSet<T> immutableHashSet = ImmutableHashSet<T>.Empty;
foreach (T uniqueKey in _keys)
{
immutableHashSet = immutableHashSet.Add(uniqueKey);
}
foreach (T uniqueKey in _keys)
{
immutableHashSet = immutableHashSet.Remove(uniqueKey);
}
return immutableHashSet;
}

[Benchmark]
public ImmutableSortedSet<T> ImmutableSortedSet()
{
ImmutableSortedSet<T> immutableSortedSet = ImmutableSortedSet<T>.Empty;
foreach (T uniqueKey in _keys)
{
immutableSortedSet = immutableSortedSet.Add(uniqueKey);
}
foreach (T uniqueKey in _keys)
{
immutableSortedSet = immutableSortedSet.Remove(uniqueKey);
}
return immutableSortedSet;
}

[Benchmark]
public ImmutableDictionary<T, T> ImmutableDictionary()
{
ImmutableDictionary<T, T> immutableDictionary = ImmutableDictionary<T, T>.Empty;
foreach (T uniqueKey in _keys)
{
immutableDictionary = immutableDictionary.Add(uniqueKey, uniqueKey);
}
foreach (T uniqueKey in _keys)
{
immutableDictionary = immutableDictionary.Remove(uniqueKey);
}
return immutableDictionary;
}

[Benchmark]
public ImmutableSortedDictionary<T, T> ImmutableSortedDictionary()
{
ImmutableSortedDictionary<T, T> immutableSortedDictionary = ImmutableSortedDictionary<T, T>.Empty;
foreach (T uniqueKey in _keys)
{
immutableSortedDictionary = immutableSortedDictionary.Add(uniqueKey, uniqueKey);
}
foreach (T uniqueKey in _keys)
{
immutableSortedDictionary = immutableSortedDictionary.Remove(uniqueKey);
}
return immutableSortedDictionary;
}

[Benchmark]
public ImmutableStack<T> ImmutableStack()
{
ImmutableStack<T> immutableStack = ImmutableStack<T>.Empty;
foreach (T uniqueKey in _keys)
{
immutableStack = immutableStack.Push(uniqueKey);
}
foreach (T uniqueKey in _keys)
{
immutableStack = immutableStack.Pop();
}
return immutableStack;
}

[Benchmark]
public ImmutableQueue<T> ImmutableQueue()
{
ImmutableQueue<T> immutableQueue = ImmutableQueue<T>.Empty;
foreach (T uniqueKey in _keys)
{
immutableQueue = immutableQueue.Enqueue(uniqueKey);
}
foreach (T uniqueKey in _keys)
{
immutableQueue = immutableQueue.Dequeue();
}
return immutableQueue;
}
}
}

0 comments on commit ac3b84b

Please sign in to comment.