[MemoryDiagnoser]
public class ExtremelySimpleDictionaryBenchmark
{
private readonly List _keys = new();
private readonly Dictionary _dict = new ();
private readonly ConcurrentDictionary _concurrentDict = new ();
private const int DictionarySize = 300;
private const string Alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static string Generate(int length, Random random)
{
var result = new char[length];
for (var i = 0; i < length; i++)
result[i] = Alphabet[random.Next(Alphabet.Length)];
return new string(result);
}
[GlobalSetup]
public void Setup()
{
var rnd = new Random();
for (var i = 0; i < DictionarySize; i++)
{
var key = Generate(6, rnd);
var count = rnd.Next(1000);
_dict[key] = count;
_concurrentDict[key] = count;
_keys.Add(key);
}
}
[Benchmark(Baseline = true)]
public int Dictionary()
{
// variable res is used to avoid dead code elimination
var res = 0;
for (var index = 0; index < _keys.Count; index++)
if(_dict.TryGetValue(_keys[index], out var value))
res += value;
return res;
}
[Benchmark]
public int ConcurrentDictionary()
{
// variable res is used to avoid dead code elimination
var res = 0;
for (var index = 0; index < _keys.Count; index++)
if(_concurrentDict.TryGetValue(_keys[index], out var value))
res += value;
return res;
}
}
< /code>
Es führt zu wirklich seltsamen Ergebnissen. ConcurrentDictionary.trygetValue
Beats Dictionary.tryGetValue in Bezug auf die Leistung. Ich habe die folgenden Ergebnisse: < /p>
Methode < /th>
Wörterbuch < /td>
2,055 US < /td>
-< /td>
na < /td>
< /tr>
< /tbody> < /> < /table> < /div>
< /tbody>
< /table> < /div>
bnchmark wurde auf dem banchmark .Net core 8, core 11. /> Wie können solche Ergebnisse erklärt werden? < /P>
Ich habe einen wirklich einfachen Benchmark zum Messen und Vergleich der Leistung von Dictionary und gleichzeitiger Verfassung : [code][MemoryDiagnoser] public class ExtremelySimpleDictionaryBenchmark { private readonly List _keys = new(); private readonly Dictionary _dict = new (); private readonly ConcurrentDictionary _concurrentDict = new ();
private static string Generate(int length, Random random) { var result = new char[length]; for (var i = 0; i < length; i++) result[i] = Alphabet[random.Next(Alphabet.Length)]; return new string(result); }
[GlobalSetup] public void Setup() { var rnd = new Random(); for (var i = 0; i < DictionarySize; i++) { var key = Generate(6, rnd); var count = rnd.Next(1000); _dict[key] = count; _concurrentDict[key] = count; _keys.Add(key); } }
[Benchmark(Baseline = true)] public int Dictionary() { // variable res is used to avoid dead code elimination var res = 0; for (var index = 0; index < _keys.Count; index++) if(_dict.TryGetValue(_keys[index], out var value)) res += value; return res; }
[Benchmark] public int ConcurrentDictionary() { // variable res is used to avoid dead code elimination var res = 0; for (var index = 0; index < _keys.Count; index++) if(_concurrentDict.TryGetValue(_keys[index], out var value)) res += value; return res; } } < /code> Es führt zu wirklich seltsamen Ergebnissen. ConcurrentDictionary.trygetValue [/code] Beats Dictionary.tryGetValue in Bezug auf die Leistung. Ich habe die folgenden Ergebnisse: < /p>
Methode < /th>
Wörterbuch < /td> 2,055 US < /td> -< /td> na < /td> < /tr> < /tbody> < /> < /table> < /div> < /tbody> < /table> < /div> bnchmark wurde auf dem banchmark .Net core 8, core 11. /> Wie können solche Ergebnisse erklärt werden? < /P>
Gemäß CPPReference sollen die meisten Verwendungen des volatilen Schlüsselworts in C ++ 20 veraltet sein. Was ist der Nachteil von flüchtigem ? Und was ist die alternative Lösung, wenn es nicht...
Ich habe versucht, die Kosten einer Indirektion zu messen, da das Stapeln von Indirektionen übereinander die Leistung speziell abbauen sollte. /> Virtuelle Funktionsaufruf. Code
#include
#include...
Also habe ich die Anweisungen hier befolgt, wie man zwei Eingaben gleichzeitig verwendet, um eine andere Funktion als einen Tastendruck einzeln auszuführen.
Mein Code möchte nicht funktionieren,...
Ich versuche, ein verschachteltes Wörterbuch in Python zu kopieren, aber wenn ich die Kopie ändere, wird auch das ursprüngliche Wörterbuch geändert.
original = {
user : {
name : Alex ,
skills :...