Created
December 28, 2021 06:05
-
-
Save mstefarov/45b7f4b5e76808fcbfba60c223966125 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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