Skip to content

Instantly share code, notes, and snippets.

@vchirikov
Created April 1, 2020 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vchirikov/466a7127d52d9dcf4216e9807332389c to your computer and use it in GitHub Desktop.
Save vchirikov/466a7127d52d9dcf4216e9807332389c to your computer and use it in GitHub Desktop.
twitter_sort_bench
using System;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
[MemoryDiagnoser]
[MinColumn, MaxColumn]
[MarkdownExporterAttribute.GitHub]
[GcServer(true)]
public class MyBenchmark
{
private readonly Dictionary<string, string> dictionary = new Dictionary<string, string>
{
{ "DestinationSubPath", "ICSharpCode.Decompiler.dll" },
{ "NuGetPackageId", "ICSharpCode.Decompiler" },
{ "AssetType", "runtime" },
{ "PackageVersion", "5.0.2.5153" },
{ "PackageName", "ICSharpCode.Decompiler" },
{ "NuGetPackageVersion", "5.0.2.5153" },
{ "CopyLocal", "true" },
{ "PathInPackage", "lib/netstandard2.0/ICSharpCode.Decompiler.dll" },
};
// 4.189 us, 260 bytes allocated
[Benchmark]
public void OrderBy()
{
var result = dictionary.OrderBy(kvp => kvp.Key);
foreach (var item in result)
{
}
}
private int Comparer((string, string) left, (string, string) right)
{
return StringComparer.OrdinalIgnoreCase.Compare(left.Item1, right.Item1);
}
// 1.047 us, 144 bytes allocated
[Benchmark]
public void SortInPlaceMethodGroup()
{
var list = new List<(string key, string value)>(dictionary.Count);
foreach (var kvp in dictionary)
{
list.Add((kvp.Key, kvp.Value));
}
list.Sort(Comparer);
foreach (var kvp in list)
{
}
}
// 1.041 us, 112 bytes allocated
[Benchmark(Baseline = true)]
public void SortInPlaceLambda()
{
var list = new List<(string key, string value)>(dictionary.Count);
foreach (var kvp in dictionary)
{
list.Add((kvp.Key, kvp.Value));
}
list.Sort((l, r) => StringComparer.OrdinalIgnoreCase.Compare(l.key, r.key));
foreach (var kvp in list)
{
}
}
private readonly Comparison<(string key, string value)> _cached = (l, r) => StringComparer.OrdinalIgnoreCase.Compare(l.key, r.key);
[Benchmark]
public void SortInLambdaCached()
{
var list = new List<(string key, string value)>(dictionary.Count);
foreach (var kvp in dictionary)
{
list.Add((kvp.Key, kvp.Value));
}
list.Sort(_cached);
foreach (var kvp in list)
{
}
}
}
internal class Program
{
private static void Main(string[] args)
{
BenchmarkDotNet.Reports.Summary summary = BenchmarkRunner.Run<MyBenchmark>();
}
}
BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18363
AMD Ryzen 9 3900X, 1 CPU, 24 logical and 12 physical cores
.NET Core SDK=3.1.300-preview-015048
[Host] : .NET Core 3.1.3 (CoreCLR 4.700.20.11803, CoreFX 4.700.20.12001), X64 RyuJIT
Job-YKKNXF : .NET Core 3.1.3 (CoreCLR 4.700.20.11803, CoreFX 4.700.20.12001), X64 RyuJIT
Server=True
| Method | Mean | Error | StdDev | Min | Max | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
|----------------------- |-----------:|---------:|---------:|-----------:|-----------:|------:|--------:|-------:|------:|------:|----------:|
| OrderBy | 1,845.7 ns | 35.05 ns | 32.79 ns | 1,809.1 ns | 1,913.6 ns | 4.62 | 0.10 | - | - | - | 632 B |
| SortInPlaceMethodGroup | 397.5 ns | 2.05 ns | 1.92 ns | 394.3 ns | 400.9 ns | 0.99 | 0.01 | 0.0010 | - | - | 248 B |
| SortInPlaceLambda | 399.6 ns | 4.16 ns | 3.69 ns | 395.0 ns | 406.2 ns | 1.00 | 0.00 | 0.0005 | - | - | 184 B |
| SortInLambdaCached | 396.0 ns | 4.28 ns | 4.00 ns | 391.9 ns | 404.2 ns | 0.99 | 0.02 | 0.0005 | - | - | 184 B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment