-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Benchmarks that search for an item in a collection.
- Loading branch information
Showing
2 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
161 changes: 161 additions & 0 deletions
161
samples/Thinktecture.Runtime.Extensions.Benchmarking/Benchmarks/ItemSearch.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Collections.ObjectModel; | ||
using System.Runtime.InteropServices; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace Thinktecture.Benchmarks; | ||
|
||
[MemoryDiagnoser] | ||
public class ItemSearch | ||
{ | ||
public class SmartEnum | ||
{ | ||
public string Key { get; } | ||
|
||
public SmartEnum(string key) | ||
{ | ||
Key = key; | ||
} | ||
} | ||
|
||
private readonly Dictionary<string, SmartEnum> _dictionary; | ||
private readonly ReadOnlyDictionary<string, SmartEnum> _readOnlyDictionary; | ||
private readonly ImmutableDictionary<string, SmartEnum> _immutableDictionary; | ||
private readonly ImmutableSortedDictionary<string, SmartEnum> _immutableSortedDictionary; | ||
private readonly List<SmartEnum> _list; | ||
private readonly ReadOnlyCollection<SmartEnum> _readOnlyCollection; | ||
private readonly SmartEnum[] _array; | ||
private readonly string[] _keysArray; | ||
|
||
[Params("aaaaAaaaaa", "iiiiIiiiii", "tttttTtttt")] | ||
public string SearchTerm { get; set; } = String.Empty; | ||
|
||
public ItemSearch() | ||
{ | ||
var dictionary = new Dictionary<string, SmartEnum>(10, StringComparer.OrdinalIgnoreCase); | ||
_immutableDictionary = ImmutableDictionary<string, SmartEnum>.Empty.WithComparers(StringComparer.OrdinalIgnoreCase); | ||
_immutableSortedDictionary = ImmutableSortedDictionary<string, SmartEnum>.Empty.WithComparers(StringComparer.OrdinalIgnoreCase); | ||
var list = new List<SmartEnum>(10); | ||
|
||
for (var i = 0; i < 20; i++) | ||
{ | ||
var key = new string((char)('a' + i), 10); | ||
var item = new SmartEnum(key); | ||
dictionary.Add(item.Key, item); | ||
_immutableDictionary = _immutableDictionary.Add(item.Key, item); | ||
_immutableSortedDictionary = _immutableSortedDictionary.Add(item.Key, item); | ||
list.Add(item); | ||
} | ||
|
||
_dictionary = dictionary; | ||
_readOnlyDictionary = new ReadOnlyDictionary<string, SmartEnum>(dictionary); | ||
_list = list; | ||
_readOnlyCollection = list.AsReadOnly(); | ||
_array = list.OrderBy(i => i.Key).ToArray(); | ||
_keysArray = _array.Select(i => i.Key).ToArray(); | ||
} | ||
|
||
[Benchmark] | ||
public SmartEnum? Dictionary() | ||
{ | ||
_dictionary.TryGetValue(SearchTerm, out var item); | ||
|
||
return item; | ||
} | ||
|
||
[Benchmark] | ||
public SmartEnum? ReadOnlyDictionary() | ||
{ | ||
_readOnlyDictionary.TryGetValue(SearchTerm, out var item); | ||
|
||
return item; | ||
} | ||
|
||
[Benchmark] | ||
public SmartEnum? ImmutableDictionary() | ||
{ | ||
_immutableDictionary.TryGetValue(SearchTerm, out var item); | ||
|
||
return item; | ||
} | ||
|
||
[Benchmark] | ||
public SmartEnum? ImmutableSortedDictionary() | ||
{ | ||
_immutableSortedDictionary.TryGetValue(SearchTerm, out var item); | ||
|
||
return item; | ||
} | ||
|
||
// [Benchmark] | ||
// public SmartEnum? List() | ||
// { | ||
// for (var i = 0; i < _list.Count; i++) | ||
// { | ||
// var item = _list[i]; | ||
// | ||
// if (SearchTerm.Equals(item.Key, StringComparison.OrdinalIgnoreCase)) | ||
// return item; | ||
// } | ||
// | ||
// return null; | ||
// } | ||
// | ||
// [Benchmark] | ||
// public SmartEnum? ListAsSpan() | ||
// { | ||
// var span = CollectionsMarshal.AsSpan(_list); | ||
// | ||
// for (var i = 0; i < span.Length; i++) | ||
// { | ||
// var item = span[i]; | ||
// | ||
// if (SearchTerm.Equals(item.Key, StringComparison.OrdinalIgnoreCase)) | ||
// return item; | ||
// } | ||
// | ||
// return null; | ||
// } | ||
|
||
// [Benchmark] | ||
// public SmartEnum? ReadOnlyList() | ||
// { | ||
// for (var i = 0; i < _readOnlyCollection.Count; i++) | ||
// { | ||
// var item = _readOnlyCollection[i]; | ||
// | ||
// if (SearchTerm.Equals(item.Key, StringComparison.OrdinalIgnoreCase)) | ||
// return item; | ||
// } | ||
// | ||
// return null; | ||
// } | ||
|
||
[Benchmark] | ||
public SmartEnum? ArrayIteration() | ||
{ | ||
for (var i = 0; i < _array.Length; i++) | ||
{ | ||
var item = _array[i]; | ||
|
||
if (SearchTerm.Equals(item.Key, StringComparison.OrdinalIgnoreCase)) | ||
return item; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
// [Benchmark] | ||
// public SmartEnum? ArrayBinary() | ||
// { | ||
// var index = Array.BinarySearch(_keysArray, SearchTerm, StringComparer.OrdinalIgnoreCase); | ||
// | ||
// if (index >= 0) | ||
// return _array[index]; | ||
// | ||
// return null; | ||
// } | ||
} |
This file contains 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