【发布时间】:2020-06-28 12:32:44
【问题描述】:
我有一个想法,尝试一下常见数据结构的访问时间,一个是Dictionary。因此,我做了一个小程序来测试访问数据需要多少滴答声,而不是直接计算值(我知道,这没什么用,只是一个实验)。
但是,我偶然发现了一个有趣的问题。我第一次访问ConcurrentDictionary 时,ElapsedTicks 总是比随后的访问时间长得多。
为什么会这样?
截图:
代码:
public static void Main() {
var aLimit = 1000;
var bLimit = aLimit;
var precomputed = new ConcurrentDictionary<Tuple<int, int>, int>();
Parallel.For(0, aLimit, a => {
Parallel.For(0, bLimit, b => {
while (!precomputed.TryAdd(new Tuple<int, int>(a, b), a * b));
});
});
Console.Write("Press enter to begin.");
Console.ReadLine();
var repeats = 5;
var stopwatch = new Stopwatch();
var random = new Random();
for (int i = 0; i < repeats; ++i) {
stopwatch.Reset();
var v1 = random.Next(0, aLimit);
var v2 = random.Next(0, bLimit);
// precomputed
stopwatch.Start();
var precomputedResult = precomputed[new Tuple<int, int>(v1, v2)];
stopwatch.Stop();
Console.WriteLine($"[{i}] V: {precomputedResult} | Precomp: {stopwatch.ElapsedTicks}");
stopwatch.Reset();
// actual
stopwatch.Start();
var actualResult = v1 * v2;
stopwatch.Stop();
Console.WriteLine($"[{i}] V: {actualResult} | Actual: {stopwatch.ElapsedTicks}");
}
}
【问题讨论】:
-
我认为这是字典访问器被 JIT 化的时候,即由 JIT 从 IL 代码编译为本机代码。
-
可能是创建初始化 ConcurrentDictionary
, int> 的抖动
标签: c# dictionary data-structures concurrency concurrentdictionary