Skip to content

Instantly share code, notes, and snippets.

@mstefarov
Created December 28, 2021 06:05
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 mstefarov/45b7f4b5e76808fcbfba60c223966125 to your computer and use it in GitHub Desktop.
Save mstefarov/45b7f4b5e76808fcbfba60c223966125 to your computer and use it in GitHub Desktop.
[MediumRunJob]
[MemoryDiagnoser]
public class StringDictBench
{
System.Collections.Generic.Dictionary<ReadOnlyMemory<char>, string> mem = new();
System.Collections.Generic.Dictionary<string, string> str = new();
string key = "key";
public StringDictBench()
{
mem.Add("e".AsMemory(), "foo");
str.Add("e", "foo");
}
private ReadOnlyMemory<char> GetMemKey() => key.AsMemory(1, 1);
private ReadOnlySpan<char> GetSpanKey() => key.AsSpan(1, 1);
[Benchmark]
public string WithMem()
{
if (mem.TryGetValue(GetMemKey(), out var result))
return result;
return null;
}
[Benchmark]
public string WithString()
{
if (str.TryGetValue(GetSpanKey().ToString(), out var result))
return result;
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment