Skip to content

Instantly share code, notes, and snippets.

@333fred
Last active May 7, 2020 04:43
Show Gist options
  • Save 333fred/2c259fff1ca0e074adead95f01db70ba to your computer and use it in GitHub Desktop.
Save 333fred/2c259fff1ca0e074adead95f01db70ba to your computer and use it in GitHub Desktop.
Small ImmutableArray vs Hashset
| Method | DesiredContainer | NumFetches | Mean | Error | StdDev |
|-------- |----------------- |----------- |---------------:|--------------:|--------------:|
| HashSet | 1 | 1 | 26.149 ns | 0.4213 ns | 0.3941 ns |
| Array | 1 | 1 | 3.068 ns | 0.0386 ns | 0.0361 ns |
| HashSet | 1 | 100 | 2,374.146 ns | 4.8138 ns | 4.5028 ns |
| Array | 1 | 100 | 275.267 ns | 1.3763 ns | 1.2201 ns |
| HashSet | 1 | 10000 | 243,664.632 ns | 341.7355 ns | 319.6596 ns |
| Array | 1 | 10000 | 27,245.051 ns | 143.4551 ns | 134.1880 ns |
| HashSet | 2 | 1 | 25.953 ns | 0.0330 ns | 0.0275 ns |
| Array | 2 | 1 | 7.641 ns | 0.0147 ns | 0.0123 ns |
| HashSet | 2 | 100 | 2,315.760 ns | 2.9523 ns | 2.4653 ns |
| Array | 2 | 100 | 801.805 ns | 2.4706 ns | 2.3110 ns |
| HashSet | 2 | 10000 | 232,675.796 ns | 2,138.9253 ns | 2,000.7521 ns |
| Array | 2 | 10000 | 82,425.552 ns | 253.1002 ns | 211.3501 ns |
| HashSet | 3 | 1 | 23.327 ns | 0.1635 ns | 0.1450 ns |
| Array | 3 | 1 | 12.608 ns | 0.0739 ns | 0.0655 ns |
| HashSet | 3 | 100 | 2,565.776 ns | 45.3442 ns | 79.4167 ns |
| Array | 3 | 100 | 1,246.539 ns | 20.6464 ns | 21.2023 ns |
| HashSet | 3 | 10000 | 236,579.193 ns | 1,071.8413 ns | 1,002.6010 ns |
| Array | 3 | 10000 | 125,318.257 ns | 1,422.8662 ns | 1,330.9500 ns |
using System.Collections.Generic;
using System.Collections.Immutable;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace MyBenchmarks
{
public class Container
{
public string Str { get; }
public Container(string s) => Str = s;
}
public class ImmutableArrayVsHashSet
{
private static KeyValuePair<string, Container> kvp(string s) => new KeyValuePair<string, Container>(s, new Container(s));
private ImmutableDictionary<string, Container> _containerHashSet = ImmutableDictionary.CreateRange(new[] { kvp("1"), kvp("2"), kvp("3") });
private ImmutableArray<Container> _containerArray = ImmutableArray.CreateRange(new[] { new Container("1"), new Container("2"), new Container("3") });
[Params("1", "2", "3")]
public string DesiredContainer;
[Params(1, 100, 10000)]
public int NumFetches;
[Benchmark]
public Container HashSet()
{
Container c = null;
for (int i = 0; i < NumFetches; i++)
{
c = _containerHashSet[DesiredContainer];
}
return c;
}
[Benchmark]
public Container Array()
{
Container c = null;
for (int i = 0; i < NumFetches; i++)
{
foreach (Container v in _containerArray)
{
if (v.Str == DesiredContainer)
{
c = v;
break;
}
}
}
return c;
}
}
public class Program
{
public static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<ImmutableArrayVsHashSet>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment