问题很简单
是这段代码
public Dictionary<string, SomeObject> values = new Dictionary<string, SomeObject>();
void Function()
{
values["foo"].a = "bar a";
values["foo"].b = "bar b";
values["foo"].c = "bar c";
values["foo"].d = "bar d";
}
与此代码相同
public Dictionary<string, SomeObject> values = new Dictionary<string, SomeObject>();
void Function()
{
var someObject = values["foo"];
someObject.a = "bar a";
someObject.b = "bar b";
someObject.c = "bar c";
someObject.d = "bar d";
}
常识告诉我,在字典中查找引用并将其存储在某处应该更快,这样就不需要多次查找,但我真的不知道字典是如何工作的。< / p>
它更快还是没有?为什么?
答案 0 :(得分:7)
然而,在现实生活中,字典查找的速度非常快,所以除非你有一本庞大的字典,否则差别不会很明显,甚至可能无法衡量。
答案 1 :(得分:6)
Joe绝对正确,但似乎还不够,我做了一个简单明显的测试:
static void Main(string[] args)
{
var dict = new Dictionary<string, Foo>();
var number = 10000000;
for (int i = 0; i < number; i++)
{
dict[i.ToString()] = new Foo();
}
var watch = new Stopwatch();
watch.Start();
for (int i = 0; i < number; i++)
{
var key = i.ToString();
dict[key].A = "a";
dict[key].B = "b";
dict[key].C = "c";
dict[key].D = "d";
}
watch.Stop();
Console.Out.WriteLine(watch.ElapsedMilliseconds);
watch.Reset();
watch.Start();
for (int i = 0; i < number; i++)
{
var key = i.ToString();
var foo = dict[key];
foo.A = "a";
foo.B = "b";
foo.C = "c";
foo.D = "d";
}
watch.Stop();
Console.Out.WriteLine(watch.ElapsedMilliseconds);
}
class Foo
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public string D { get; set; }
}
在我的机器上输出到:
3423
2113
Press any key to continue . . .
只有1次查询肯定会减少大数字的总时间。
答案 2 :(得分:1)
我很好奇。以下单元测试(可能)显示第二种方法的速度提高了约25%。 (121毫秒vs 91毫秒)。从6个领域到2个领域缩小了差距,40毫秒vs 33毫秒。我说可能是因为我写得相当快,我不相信它可以免于测量一些副作用,但它显示了预期的行为,所以为什么要质疑它。 (HAH)。
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
namespace TestProject1
{
public class DataObject
{
public string A { get; set; }
public string B { get; set; }
public string C { get; set; }
public string D { get; set; }
public string E { get; set; }
public string F { get; set; }
}
[TestClass]
public class UnitTest1
{
public static Dictionary<string, DataObject> dict = new Dictionary<string, DataObject>();
static string lookie;
[ClassInitialize()]
public static void MyClassInitialize(TestContext testContext) {
Random rand = new Random(123545);
for (int i = 0; i < 10000; i++)
{
string key = rand.NextDouble().ToString();
DataObject dob = new DataObject();
dict.Add(key, dob);
if (i == 4567)
lookie = key;
}
}
[TestMethod]
public void TestMethod()
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int j = 0; j < 100000; j++)
{
dict[lookie].A = "val" + j;
dict[lookie].B = "val" + j;
dict[lookie].C = "val" + j;
dict[lookie].D = "val" + j;
dict[lookie].E = "val" + j;
dict[lookie].F = "val" + j;
}
sw.Stop();
System.Diagnostics.Debug.WriteLine(sw.ElapsedMilliseconds);
sw.Reset();
sw.Start();
for (int j = 0; j < 100000; j++)
{
DataObject dob = dict[lookie];
dob.A = "val" + j;
dob.B = "val" + j;
dob.C = "val" + j;
dob.D = "val" + j;
dob.E = "val" +j;
dob.F = "val" +j;
}
sw.Stop();
System.Diagnostics.Debug.WriteLine(sw.ElapsedMilliseconds);
}
}
}