Skip to content

Instantly share code, notes, and snippets.

@ayende
Last active June 27, 2024 07:27
Show Gist options
  • Save ayende/307d50cac75ab3c5819568f9e5f68644 to your computer and use it in GitHub Desktop.
Save ayende/307d50cac75ab3c5819568f9e5f68644 to your computer and use it in GitHub Desktop.
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Collections.Frozen;
using System.Collections.Immutable;
using System.IO.MemoryMappedFiles;
using System.Text;
BenchmarkRunner.Run<Dics>();
public class Dics
{
const int OperationsCount = 256;
const int TransactionsCount = 8*1024;
[Benchmark(Baseline = true )]
public int SingleDictionaryBench() => SingleDictionary().Count();
static IEnumerable<object> SingleDictionary()
{
var dic = new Dictionary<long, object>();
var random = new Random(932);
var v = new object();
// number of transactions
for (var txCount = 0; txCount < TransactionsCount; txCount++)
{
// operations in transaction
for (int opCount = 0; opCount < OperationsCount; opCount++)
{
dic[random.NextInt64(0, 1024 * 1024 * 1024)] = v;
}
yield return dic;// publish the dictionary
}
}
[Benchmark()]
public int ClonedDictionaryBench() => ClonedDictionary().Count();
static IEnumerable<object> ClonedDictionary()
{
var dic = new Dictionary<long, object>();
var random = new Random(932);
var v = new object();
// number of transactions
for (var txCount = 0; txCount < TransactionsCount; txCount++)
{
// operations in transaction
for (int opCount = 0; opCount < OperationsCount; opCount++)
{
dic[random.NextInt64(0, 1024 * 1024 * 1024)] = v;
}
yield return new Dictionary<long, object>(dic);// publish the dictionary
}
}
[Benchmark()]
public int ImmutableDictionaryFromDicBench() => ImmutableDictionaryFromDic().Count();
static IEnumerable<object> ImmutableDictionaryFromDic()
{
var dic = new Dictionary<long, object>();
var random = new Random(932);
var v = new object();
// number of transactions
for (var txCount = 0; txCount < TransactionsCount; txCount++)
{
// operations in transaction
for (int opCount = 0; opCount < OperationsCount; opCount++)
{
dic[random.NextInt64(0, 1024 * 1024 * 1024)] = v;
}
// publish the dictionary
yield return dic.ToImmutableDictionary();
}
}
[Benchmark()]
public int FrozenDictionaryBench() => FrozenDictionary().Count();
static IEnumerable<object> FrozenDictionary()
{
var dic = new Dictionary<long, object>();
var random = new Random(932);
var v = new object();
// number of transactions
for (var txCount = 0; txCount < TransactionsCount; txCount++)
{
// operations in transaction
for (int opCount = 0; opCount < OperationsCount; opCount++)
{
dic[random.NextInt64(0, 1024 * 1024 * 1024)] = v;
}
// publish the dictionary
yield return dic.ToFrozenDictionary();
}
}
[Benchmark()]
public int BuilderImmutableDictionaryBench() => BuilderImmutableDictionary().Count();
static IEnumerable<object> BuilderImmutableDictionary()
{
var builder = ImmutableDictionary.CreateBuilder<long, object>();
var random = new Random(932);
var v = new object();
// number of transactions
for (var txCount = 0; txCount < TransactionsCount; txCount++)
{
// operations in transaction
for (int opCount = 0; opCount < OperationsCount; opCount++)
{
builder[random.NextInt64(0, 1024 * 1024 * 1024)] = v;
}
// publish the dictionary
yield return builder.ToImmutable();
}
}
[Benchmark()]
public int ClonedImmutableDictionaryBench() => ClonedImmutableDictionary().Count();
static IEnumerable<object> ClonedImmutableDictionary()
{
var dic = ImmutableDictionary.Create<long, object>();
var random = new Random(932);
var v = new object();
// number of transactions
for (var txCount = 0; txCount < TransactionsCount; txCount++)
{
// operations in transaction
for (int opCount = 0; opCount < OperationsCount; opCount++)
{
dic = dic.Add(random.NextInt64(0, 1024 * 1024 * 1024), v);
}
// publish the dictionary
yield return dic;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment