Skip to content

Instantly share code, notes, and snippets.

@taddison
Last active September 1, 2021 19:19
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 taddison/e4237e7aafc5e0066638b44e1948c27b to your computer and use it in GitHub Desktop.
Save taddison/e4237e7aafc5e0066638b44e1948c27b to your computer and use it in GitHub Desktop.
Cache Key construction benchmark

How long does it take to construct that cache key?

dotnet run -c Release -f net6.0 --runtimes net48 net5.0 net6.0 --filter *
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net48;net5.0;net6.0</TargetFrameworks>
<RootNamespace>caching_key</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
</ItemGroup>
</Project>
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Attributes;
using Microsoft.Extensions.Caching.Memory;
public class Program
{
public static void Main(string[] args) =>
BenchmarkSwitcher.FromAssemblies(new[] { typeof(Program).Assembly }).Run(args);
}
[MemoryDiagnoser]
public class CacheKeyBenchmarks
{
IMemoryCache _cache;
private const string SEPARATOR = "_";
private const string USER_CACHE_ID = "USER";
public CacheKeyBenchmarks()
{
_cache = new MemoryCache(new MemoryCacheOptions());
}
[Benchmark]
[Arguments(1)]
public bool CheckItemExists_Hardcoded(int key)
{
return _cache.TryGetValue("USER_1234", out object _);
}
[Benchmark]
[Arguments(1)]
public bool CheckItemExists_Object_ToString(int key)
{
return _cache.TryGetValue(new { cacheId = "USER", userKey = key }.ToString(), out object _);
}
[Benchmark]
[Arguments(1)]
public bool CheckItemExists_Interpolated(int key)
{
return _cache.TryGetValue($"USER_{key}", out object _);
}
[Benchmark]
[Arguments(1)]
public bool CheckItemExists_Join(int key)
{
return _cache.TryGetValue(string.Join("_", "USER", key), out object _);
}
[Benchmark]
[Arguments(1)]
public bool CheckItemExists_Join_Consts(int key)
{
return _cache.TryGetValue(string.Join(SEPARATOR, USER_CACHE_ID, key), out object _);
}
[Benchmark]
[Arguments(1)]
public bool CheckItemExists_Concat(int key)
{
return _cache.TryGetValue(string.Concat(USER_CACHE_ID, "_", key), out object _);
}
[Benchmark]
[Arguments(1)]
public bool CheckItemExists_Concat_Consts(int key)
{
return _cache.TryGetValue(string.Concat(USER_CACHE_ID, SEPARATOR, key), out object _);
}
}
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19043.1202 (21H1/May2021Update)
Intel Core i7-8650U CPU 1.90GHz (Kaby Lake R), 1 CPU, 8 logical and 4 physical cores
.NET SDK=6.0.100-preview.7.21379.14
  [Host]     : .NET 6.0.0 (6.0.21.37719), X64 RyuJIT
  Job-THNXQL : .NET 5.0.9 (5.0.921.35908), X64 RyuJIT
  Job-TGUSBV : .NET 6.0.0 (6.0.21.37719), X64 RyuJIT
  Job-XADQDX : .NET Framework 4.8 (4.8.4400.0), X64 RyuJIT

Method Job Runtime Toolchain key Mean Error StdDev Ratio RatioSD Gen 0 Allocated
CheckItemExists_Hardcoded Job-THNXQL .NET 5.0 net5.0 1 121.11 ns 1.858 ns 4.696 ns 0.84 0.05 - -
CheckItemExists_Hardcoded Job-TGUSBV .NET 6.0 net6.0 1 65.47 ns 1.324 ns 1.173 ns 0.45 0.01 - -
CheckItemExists_Hardcoded Job-XADQDX .NET Framework 4.8 net48 1 146.30 ns 2.858 ns 3.614 ns 1.00 0.00 - -
CheckItemExists_Object_ToString Job-THNXQL .NET 5.0 net5.0 1 325.64 ns 6.374 ns 7.588 ns 0.62 0.02 0.0381 160 B
CheckItemExists_Object_ToString Job-TGUSBV .NET 6.0 net6.0 1 224.89 ns 3.869 ns 3.231 ns 0.43 0.01 0.0381 160 B
CheckItemExists_Object_ToString Job-XADQDX .NET Framework 4.8 net48 1 525.49 ns 7.121 ns 5.560 ns 1.00 0.00 0.0458 193 B
CheckItemExists_Interpolated Job-THNXQL .NET 5.0 net5.0 1 196.34 ns 2.763 ns 5.052 ns 0.62 0.02 0.0153 64 B
CheckItemExists_Interpolated Job-TGUSBV .NET 6.0 net6.0 1 116.02 ns 2.222 ns 2.079 ns 0.37 0.01 0.0095 40 B
CheckItemExists_Interpolated Job-XADQDX .NET Framework 4.8 net48 1 317.94 ns 4.203 ns 3.510 ns 1.00 0.00 0.0229 96 B
CheckItemExists_Join Job-THNXQL .NET 5.0 net5.0 1 180.07 ns 3.626 ns 5.645 ns 0.61 0.02 0.0248 104 B
CheckItemExists_Join Job-TGUSBV .NET 6.0 net6.0 1 108.13 ns 2.127 ns 2.449 ns 0.37 0.01 0.0248 104 B
CheckItemExists_Join Job-XADQDX .NET Framework 4.8 net48 1 296.09 ns 5.705 ns 6.791 ns 1.00 0.00 0.0324 136 B
CheckItemExists_Join_Consts Job-THNXQL .NET 5.0 net5.0 1 179.67 ns 3.618 ns 4.829 ns 0.60 0.02 0.0248 104 B
CheckItemExists_Join_Consts Job-TGUSBV .NET 6.0 net6.0 1 108.76 ns 2.202 ns 3.493 ns 0.36 0.01 0.0248 104 B
CheckItemExists_Join_Consts Job-XADQDX .NET Framework 4.8 net48 1 301.91 ns 6.076 ns 7.684 ns 1.00 0.00 0.0324 136 B
CheckItemExists_Concat Job-THNXQL .NET 5.0 net5.0 1 159.32 ns 3.064 ns 4.948 ns 0.60 0.02 0.0153 64 B
CheckItemExists_Concat Job-TGUSBV .NET 6.0 net6.0 1 94.54 ns 1.898 ns 1.683 ns 0.35 0.01 0.0153 64 B
CheckItemExists_Concat Job-XADQDX .NET Framework 4.8 net48 1 266.56 ns 3.404 ns 3.018 ns 1.00 0.00 0.0229 96 B
CheckItemExists_Concat_Consts Job-THNXQL .NET 5.0 net5.0 1 158.23 ns 3.034 ns 2.533 ns 0.61 0.02 0.0153 64 B
CheckItemExists_Concat_Consts Job-TGUSBV .NET 6.0 net6.0 1 95.32 ns 1.939 ns 2.841 ns 0.36 0.01 0.0153 64 B
CheckItemExists_Concat_Consts Job-XADQDX .NET Framework 4.8 net48 1 260.36 ns 5.211 ns 5.576 ns 1.00 0.00 0.0229 96 B
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19043.1202 (21H1/May2021Update)
Intel Core i7-8650U CPU 1.90GHz (Kaby Lake R), 1 CPU, 8 logical and 4 physical cores
.NET SDK=5.0.206
  [Host]     : .NET 5.0.9 (5.0.921.35908), X64 RyuJIT
  DefaultJob : .NET 5.0.9 (5.0.921.35908), X64 RyuJIT

Method key Mean Error StdDev Gen 0 Allocated
CheckItemExists_Hardcoded 1 115.2 ns 2.29 ns 3.56 ns - -
CheckItemExists_Object_ToString 1 323.1 ns 6.20 ns 15.55 ns 0.0381 160 B
CheckItemExists_Interpolated 1 193.8 ns 3.81 ns 4.96 ns 0.0153 64 B
CheckItemExists_Join 1 179.4 ns 3.48 ns 4.27 ns 0.0248 104 B
CheckItemExists_Join_Consts 1 180.6 ns 3.63 ns 4.72 ns 0.0248 104 B
CheckItemExists_Concat 1 159.7 ns 3.16 ns 3.99 ns 0.0153 64 B
CheckItemExists_Concat_Consts 1 157.6 ns 3.03 ns 3.49 ns 0.0153 64 B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment