我的印象是,在词典中查找项目而不是列表更快,以下代码似乎暗示:
词典:66滴答
列表:32个刻度
我假设我已经搞砸了?
static void Main(string[] args)
{
// Speed test.
Dictionary<string, int> d = new Dictionary<string, int>()
{
{"P1I1-1MS P2I1-1MS 3I-1MS 4I-1MS", 2},
{"P1I2-1MS P2I1-1MS 3I-1MS 4I-1MS", 1},
{"P1I3-1MS P2I1-1MS 3I-1MS 4I-1MS", 0},
{"P1I4-1MS P2I1-1MS 3I-1MS 4I-1MS", -1},
{"P1I5-1MS P2I1-1MS 3I-1MS 4I-1MS", 0},
{"P1I1-1MS P2I2-1MS 3I-1MS 4I-1MS", 0},
{"P1I2-1MS P2I2-1MS 3I-1MS 4I-1MS", 0},
{"P1I3-1MS P2I2-1MS 3I-1MS 4I-1MS", 0},
{"P1I4-1MS P2I2-1MS 3I-1MS 4I-1MS", 0},
{"P1I5-1MS P2I2-1MS 3I-1MS 4I-1MS", 0},
{"P1I1-1MS P2I3-1MS 3I-1MS 4I-1MS", 0},
{"P1I2-1MS P2I3-1MS 3I-1MS 4I-1MS", 0},
{"P1I3-1MS P2I3-1MS 3I-1MS 4I-1MS", 0},
{"P1I4-1MS P2I3-1MS 3I-1MS 4I-1MS", 0},
{"P1I5-1MS P2I3-1MS 3I-1MS 4I-1MS", 0},
{"P1I1-1MS P2I4-1MS 3I-1MS 4I-1MS", 2}
};
List<string> l = new List<string>();
l.Add("P1I1-1MS P2I1-1MS 3I-1MS 4I-1MS");
l.Add("P1I2-1MS P2I1-1MS 3I-1MS 4I-1MS");
l.Add("P1I3-1MS P2I1-1MS 3I-1MS 4I-1MS");
l.Add("P1I4-1MS P2I1-1MS 3I-1MS 4I-1MS");
l.Add("P1I5-1MS P2I1-1MS 3I-1MS 4I-1MS");
l.Add("P1I1-1MS P2I2-1MS 3I-1MS 4I-1MS");
l.Add("P1I2-1MS P2I2-1MS 3I-1MS 4I-1MS");
l.Add("P1I3-1MS P2I2-1MS 3I-1MS 4I-1MS");
l.Add("P1I4-1MS P2I2-1MS 3I-1MS 4I-1MS");
l.Add("P1I5-1MS P2I2-1MS 3I-1MS 4I-1MS");
l.Add("P1I1-1MS P2I3-1MS 3I-1MS 4I-1MS");
l.Add("P1I2-1MS P2I3-1MS 3I-1MS 4I-1MS");
l.Add("P1I3-1MS P2I3-1MS 3I-1MS 4I-1MS");
l.Add("P1I4-1MS P2I3-1MS 3I-1MS 4I-1MS");
l.Add("P1I5-1MS P2I3-1MS 3I-1MS 4I-1MS");
l.Add("P1I1-1MS P2I4-1MS 3I-1MS 4I-1MS");
Stopwatch sw = new Stopwatch();
string temp = "P1I1-1MS P2I4-1MS 3I-1MS 4I-1MS";
bool inDictionary = false;
sw.Start();
if (d.ContainsKey(temp))
{
sw.Stop();
inDictionary = true;
}
else sw.Reset();
Console.WriteLine(sw.ElapsedTicks.ToString());
Console.WriteLine(inDictionary.ToString());
bool inList = false;
sw.Reset();
sw.Start();
if (l.Contains(temp))
{
sw.Stop();
inList = true;
}
else sw.Reset();
Console.WriteLine(sw.ElapsedTicks.ToString());
Console.WriteLine(inList.ToString());
Console.ReadLine();
}
修改
对Matthew Watson代码的修改。
答案 0 :(得分:4)
这是一个合适的测试:
Dictionary<string, int> d = new Dictionary<string, int>()
{
{"P1I1-1MS P2I1-1MS 3I-1MS 4I-1MS", 2},
{"P1I2-1MS P2I1-1MS 3I-1MS 4I-1MS", 1},
{"P1I3-1MS P2I1-1MS 3I-1MS 4I-1MS", 0},
{"P1I4-1MS P2I1-1MS 3I-1MS 4I-1MS", -1},
{"P1I5-1MS P2I1-1MS 3I-1MS 4I-1MS", 0},
{"P1I1-1MS P2I2-1MS 3I-1MS 4I-1MS", 0},
{"P1I2-1MS P2I2-1MS 3I-1MS 4I-1MS", 0},
{"P1I3-1MS P2I2-1MS 3I-1MS 4I-1MS", 0},
{"P1I4-1MS P2I2-1MS 3I-1MS 4I-1MS", 0},
{"P1I5-1MS P2I2-1MS 3I-1MS 4I-1MS", 0},
{"P1I1-1MS P2I3-1MS 3I-1MS 4I-1MS", 0},
{"P1I2-1MS P2I3-1MS 3I-1MS 4I-1MS", 0},
{"P1I3-1MS P2I3-1MS 3I-1MS 4I-1MS", 0},
{"P1I4-1MS P2I3-1MS 3I-1MS 4I-1MS", 0},
{"P1I5-1MS P2I3-1MS 3I-1MS 4I-1MS", 0},
{"P1I1-1MS P2I4-1MS 3I-1MS 4I-1MS", 2}
};
List<string> l = new List<string>
{
"P1I1-1MS P2I1-1MS 3I-1MS 4I-1MS",
"P1I2-1MS P2I1-1MS 3I-1MS 4I-1MS",
"P1I3-1MS P2I1-1MS 3I-1MS 4I-1MS",
"P1I4-1MS P2I1-1MS 3I-1MS 4I-1MS",
"P1I5-1MS P2I1-1MS 3I-1MS 4I-1MS",
"P1I1-1MS P2I2-1MS 3I-1MS 4I-1MS",
"P1I2-1MS P2I2-1MS 3I-1MS 4I-1MS",
"P1I3-1MS P2I2-1MS 3I-1MS 4I-1MS",
"P1I4-1MS P2I2-1MS 3I-1MS 4I-1MS",
"P1I5-1MS P2I2-1MS 3I-1MS 4I-1MS",
"P1I1-1MS P2I3-1MS 3I-1MS 4I-1MS",
"P1I2-1MS P2I3-1MS 3I-1MS 4I-1MS",
"P1I3-1MS P2I3-1MS 3I-1MS 4I-1MS",
"P1I4-1MS P2I3-1MS 3I-1MS 4I-1MS",
"P1I5-1MS P2I3-1MS 3I-1MS 4I-1MS",
"P1I1-1MS P2I4-1MS 3I-1MS 4I-1MS"
};
int trials = 4;
int iters = 1000000;
Stopwatch sw = new Stopwatch();
string target = "P1I1-1MS P2I4-1MS 3I-1MS 4I-1MS";
for (int trial = 0; trial < trials; ++trial)
{
sw.Restart();
for (int i = 0; i < iters; ++i)
d.ContainsKey(target);
sw.Stop();
Console.WriteLine("Dictionary took " + sw.Elapsed);
sw.Restart();
for (int i = 0; i < iters; ++i)
l.Contains(target);
sw.Stop();
Console.WriteLine("List took " + sw.Elapsed);
}
运行此OUTSIDE任何调试器的RELEASE版本。
我的结果:
Dictionary took 00:00:00.0587588
List took 00:00:00.2018361
Dictionary took 00:00:00.0578586
List took 00:00:00.2003057
Dictionary took 00:00:00.0611053
List took 00:00:00.2033325
Dictionary took 00:00:00.0583175
List took 00:00:00.2056591
字典显然更快,即使条目很少。
使用更多条目,与列表相比,词典会更快。
使用Dictionary的查找时间是O(1),而List是O(N)。这当然会对N的大值产生巨大的差异。
答案 1 :(得分:2)
字典 上);它意味着有一个大小X,在这个大小X上,Dictionary开始比List快。例如。如果词典/列表包含10000个或更多项目,则词典总是更快(如果更少要存储的项目,比如5,列表可以更快)。 Dictionary还有另一个问题:它需要 GetHashCode()方法;如果GetHashCode()实现不好,Dictionary可能会非常慢。
答案 2 :(得分:2)
这是由于law of large numbers。简而言之
根据法律规定,从大的平均结果中获得 试验次数应接近预期值,并且趋于一致 随着更多的试验进行而越来越近。
另一个约束是Big-O notation ,实际上在小规模上是无用的。例如,对于给定的 n ,可以说O(1)~O(N)~O(n!)小于某个小数。
运行一个好的实验需要满足一些非常严格的条件,例如: