|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using BenchmarkDotNet.Attributes; |
| 8 | +using MicroBenchmarks; |
| 9 | + |
| 10 | +namespace System.Collections.Tests |
| 11 | +{ |
| 12 | + [BenchmarkCategory(Categories.CoreFX, Categories.Collections, Categories.GenericCollections)] |
| 13 | + public class DictionarySequentialKeys |
| 14 | + { |
| 15 | + private const int Seventeen = 17; |
| 16 | + private const int ThreeThousand = 3_000; |
| 17 | + |
| 18 | + private Dictionary<int, int> _dict_3k; |
| 19 | + private Dictionary<int, (long, long, long, long)> _dict32ByteValue_3k; |
| 20 | + private Dictionary<int, (object, object, object, object)> _dict32ByteRefsValue_3k; |
| 21 | + |
| 22 | + private Dictionary<int, int> _dict_17; |
| 23 | + private Dictionary<int, (long, long, long, long)> _dict32ByteValue_17; |
| 24 | + private Dictionary<int, (object, object, object, object)> _dict32ByteRefsValue_17; |
| 25 | + |
| 26 | + [GlobalSetup] |
| 27 | + public void Initialize() |
| 28 | + { |
| 29 | + (object, object, object, object) item = (new object(), new object(), new object(), new object()); |
| 30 | + |
| 31 | + _dict_17 = Enumerable.Range(0, Seventeen).ToDictionary(i => i); |
| 32 | + _dict32ByteValue_17 = Enumerable.Range(0, Seventeen).ToDictionary(i => i, i => default((long, long, long, long))); |
| 33 | + _dict32ByteRefsValue_17 = Enumerable.Range(0, Seventeen).ToDictionary(i => i, i => item); |
| 34 | + |
| 35 | + _dict_3k = Enumerable.Range(0, ThreeThousand).ToDictionary(i => i); |
| 36 | + _dict32ByteValue_3k = Enumerable.Range(0, ThreeThousand).ToDictionary(i => i, i => default((long, long, long, long))); |
| 37 | + _dict32ByteRefsValue_3k = Enumerable.Range(0, ThreeThousand).ToDictionary(i => i, i => item); |
| 38 | + } |
| 39 | + |
| 40 | + [Benchmark(OperationsPerInvoke = Seventeen)] |
| 41 | + public int ContainsValue_17_Int_Int() |
| 42 | + => ContainsKey_Int_Int(_dict_17, Seventeen); |
| 43 | + |
| 44 | + [Benchmark(OperationsPerInvoke = Seventeen)] |
| 45 | + public int ContainsKey_17_Int_32ByteValue() |
| 46 | + => ContainsKey_Int_LargeStruct(_dict32ByteValue_17, Seventeen); |
| 47 | + |
| 48 | + [Benchmark(OperationsPerInvoke = Seventeen)] |
| 49 | + public int ContainsKey_17_Int_32ByteRefsValue() |
| 50 | + => ContainsKey_Int_LargeRefStruct(_dict32ByteRefsValue_17, Seventeen); |
| 51 | + |
| 52 | + |
| 53 | + [Benchmark(OperationsPerInvoke = ThreeThousand)] |
| 54 | + public int ContainsValue_3k_Int_Int() |
| 55 | + => ContainsKey_Int_Int(_dict_3k, ThreeThousand); |
| 56 | + |
| 57 | + [Benchmark(OperationsPerInvoke = ThreeThousand)] |
| 58 | + public int ContainsKey_3k_Int_32ByteValue() |
| 59 | + => ContainsKey_Int_LargeStruct(_dict32ByteValue_3k, ThreeThousand); |
| 60 | + |
| 61 | + [Benchmark(OperationsPerInvoke = ThreeThousand)] |
| 62 | + public int ContainsKey_3k_Int_32ByteRefsValue() |
| 63 | + => ContainsKey_Int_LargeRefStruct(_dict32ByteRefsValue_3k, ThreeThousand); |
| 64 | + |
| 65 | + |
| 66 | + private static int ContainsKey_Int_Int(Dictionary<int, int> d, int count) |
| 67 | + { |
| 68 | + int total = 0; |
| 69 | + |
| 70 | + for (int i = 0; i < count; i++) |
| 71 | + { |
| 72 | + if (d.ContainsKey(i)) |
| 73 | + { |
| 74 | + total++; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return total; |
| 79 | + } |
| 80 | + |
| 81 | + private static int ContainsKey_Int_LargeStruct(Dictionary<int, (long, long, long, long)> d, int count) |
| 82 | + { |
| 83 | + int total = 0; |
| 84 | + |
| 85 | + for (int i = 0; i < count; i++) |
| 86 | + { |
| 87 | + if (d.ContainsKey(i)) |
| 88 | + { |
| 89 | + total++; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return total; |
| 94 | + } |
| 95 | + |
| 96 | + private static int ContainsKey_Int_LargeRefStruct(Dictionary<int, (object, object, object, object)> d, int count) |
| 97 | + { |
| 98 | + int total = 0; |
| 99 | + |
| 100 | + for (int i = 0; i < count; i++) |
| 101 | + { |
| 102 | + if (d.ContainsKey(i)) |
| 103 | + { |
| 104 | + total++; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return total; |
| 109 | + } |
| 110 | + |
| 111 | + [Benchmark(OperationsPerInvoke = Seventeen)] |
| 112 | + public int TryGetValue_17_Int_Int() |
| 113 | + => TryGetValue_Int_Int(_dict_17, Seventeen); |
| 114 | + |
| 115 | + [Benchmark(OperationsPerInvoke = Seventeen)] |
| 116 | + public int TryGetValue_17_Int_32ByteValue() |
| 117 | + => TryGetValue_Int_LargeStruct(_dict32ByteValue_17, Seventeen); |
| 118 | + |
| 119 | + [Benchmark(OperationsPerInvoke = Seventeen)] |
| 120 | + public int TryGetValue_17_Int_32ByteRefsValue() |
| 121 | + => TryGetValue_Int_LargeRefStruct(_dict32ByteRefsValue_17, Seventeen); |
| 122 | + |
| 123 | + |
| 124 | + [Benchmark(OperationsPerInvoke = ThreeThousand)] |
| 125 | + public int TryGetValue_3k_Int_Int() |
| 126 | + => TryGetValue_Int_Int(_dict_3k, ThreeThousand); |
| 127 | + |
| 128 | + [Benchmark(OperationsPerInvoke = ThreeThousand)] |
| 129 | + public int TryGetValue_3k_Int_32ByteValue() |
| 130 | + => TryGetValue_Int_LargeStruct(_dict32ByteValue_3k, ThreeThousand); |
| 131 | + |
| 132 | + [Benchmark(OperationsPerInvoke = ThreeThousand)] |
| 133 | + public int TryGetValue_3k_Int_32ByteRefsValue() |
| 134 | + => TryGetValue_Int_LargeRefStruct(_dict32ByteRefsValue_3k, ThreeThousand); |
| 135 | + |
| 136 | + private static int TryGetValue_Int_Int(Dictionary<int, int> d, int count) |
| 137 | + { |
| 138 | + int total = 0; |
| 139 | + |
| 140 | + for (int i = 0; i < count; i++) |
| 141 | + { |
| 142 | + if (d.TryGetValue(i, out _)) |
| 143 | + { |
| 144 | + total++; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + return total; |
| 149 | + } |
| 150 | + |
| 151 | + private static int TryGetValue_Int_LargeStruct(Dictionary<int, (long, long, long, long)> d, int count) |
| 152 | + { |
| 153 | + int total = 0; |
| 154 | + |
| 155 | + for (int i = 0; i < count; i++) |
| 156 | + { |
| 157 | + if (d.TryGetValue(i, out _)) |
| 158 | + { |
| 159 | + total++; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return total; |
| 164 | + } |
| 165 | + |
| 166 | + private static int TryGetValue_Int_LargeRefStruct(Dictionary<int, (object, object, object, object)> d, int count) |
| 167 | + { |
| 168 | + int total = 0; |
| 169 | + |
| 170 | + for (int i = 0; i < count; i++) |
| 171 | + { |
| 172 | + if (d.TryGetValue(i, out _)) |
| 173 | + { |
| 174 | + total++; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + return total; |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments