近期做版本迭代任务,有一个在店铺头部展示店主所在的城市名称和省份名称的需求,店主信息表中保存了店主所在的城市Id和省份Id,由于原有业务复杂,要尽量减少Sql执行时间,所以不考虑join城市地区详细表。于是考虑在集合类中处理。
是选择Hashtable还是Dictionary<T,T>呢?于是做了一个测试,代码清单如下:
/// <summary>
/// 城市信息
/// </summary>
public class CityInfo
{
public string CityName { get; set; }
public string ProvinceName { get; set; }
}
/// <summary>
/// 数据仓库和查找方法
/// </summary>
public static class Respository
{ public static Hashtable Retrieve()
{
Hashtable data = new Hashtable();
for (int i = ; i < ; i++)
{
data.Add(i+,"data"+i);
}
return data;
}
public static string Find(Hashtable data,int id)
{
var query = from item in data.Cast<DictionaryEntry>() where (int)item.Key == id select item.Value.ToString();
return query.First();
} public static Dictionary<int, string> SecondRetrieve() {
Dictionary<int,string> data = new Dictionary<int,string>();
for (int i = ; i < ; i++)
{
data.Add(i + , "data" + i);
}
return data;
} public static string SecondFind(Dictionary<int, string> data, int id)
{
var query = from item in data where (int)item.Key == id select item.Value;
return query.First();
} public static Dictionary<int, CityInfo> ThridRetrieve()
{
Dictionary<int, CityInfo> data = new Dictionary<int, CityInfo>();
for (int i = ; i < ; i++)
{
data.Add(i + , new CityInfo { CityName="CityName"+i, ProvinceName="ProvinceName"+i });
}
return data;
} public static CityInfo ThridFind(Dictionary<int, CityInfo> data, int id)
{
var query = from item in data where (int)item.Key == id select item.Value;
return query.First();
}
}
测试入口:
static void Main(string[] args)
{
{
Stopwatch stwtotal = new Stopwatch();
stwtotal.Start();
Hashtable data = Respository.Retrieve();
for (int i = ; i < ; i++)
{
Stopwatch stw = new Stopwatch();
stw.Start();
Random r = new Random();
string result = Respository.Find(data, r.Next(, )*i);
stw.Stop();
Console.WriteLine(result + " FirstFindExecuteTime:" + stw.Elapsed.TotalMilliseconds+"ms");
}
stwtotal.Stop();
Console.WriteLine("测试数据总时间:" + stwtotal.Elapsed.TotalMilliseconds + "ms");
}
{
Stopwatch stwtotal = new Stopwatch();
stwtotal.Start();
Dictionary<int, string> data = Respository.SecondRetrieve();
for (int i = ; i < ; i++)
{
Stopwatch stw = new Stopwatch();
stw.Start();
Random r = new Random();
string result = Respository.SecondFind(data, r.Next(, )*i);
stw.Stop();
Console.WriteLine(result + " SecondFindExecuteTime:" + stw.Elapsed.TotalMilliseconds+"ms");
}
stwtotal.Stop();
Console.WriteLine("测试数据总时间:" + stwtotal.Elapsed.TotalMilliseconds + "ms");
}
{
Stopwatch stwtotal = new Stopwatch();
stwtotal.Start();
Dictionary<int, CityInfo> data = Respository.ThridRetrieve();
for (int i = ; i < ; i++)
{
Stopwatch stw = new Stopwatch();
stw.Start();
Random r = new Random();
CityInfo result = Respository.ThridFind(data, r.Next(, ) * i);
stw.Stop();
Console.WriteLine(result.CityName + " ThridFindExecuteTime:" + stw.Elapsed.TotalMilliseconds + "ms");
}
stwtotal.Stop();
Console.WriteLine("真实数据初始化到查找结束总时间:" + stwtotal.Elapsed.TotalMilliseconds + "ms");
}
Console.ReadKey();
}
测试结论:Dictionary<T,T>明显优于Hashtable
截图如下:
1.Hashtable
2Dictionary<int,string>
3,Dictionary<int,CityInfo>