using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static List<Boo> pool = new List<Boo>();
static List<Boo> shuffled;
static Dictionary<string, Boo> dict;
static Dictionary<string, string> strDict;
static void Main(string[] args)
{
int MAX_NO = 100;
//使用相口同亂數種子確保每次執行之測試資料相同
Random rnd = new Random(9527); //交給你了,9527
//建立大量物件集合
for (int i = 0; i < MAX_NO; i++)
{
for (int j = 0; j < rnd.Next(500, 1000); j++)
{
pool.Add(new Boo()
{
No = i,
SubNo = j,
Code = "C" + rnd.Next(1000).ToString("000")
});
}
}
//打亂排序
shuffled = pool.OrderBy(o => rnd.Next()).ToList();
//建立Dictionary
dict = pool.ToDictionary(
o => string.Format("{0}\t{1}", o.No, o.SubNo),
o => o);
//建立字串Dictionary
strDict = pool.ToDictionary(
o => string.Format("{0}\t{1}", o.No, o.SubNo),
o => o.Code);
//產生TIMES個待查對象
int TIMES = 500;
List<Boo> toFill = new List<Boo>();
for (int i = 0; i < TIMES; i++)
{
Boo sample = pool[rnd.Next(pool.Count)];
toFill.Add(new Boo()
{
No = sample.No, SubNo = sample.SubNo
});
}
Console.WriteLine("Count={0:N0}", pool.Count);
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Round {0}", i);
Stopwatch sw = new Stopwatch();
sw.Start();
for (int j = 0; j < TIMES; j++)
{
var boo = toFill[j];
//分別用GetCode1到GetCode4測試
boo.Code = GetCode4(boo.No, boo.SubNo);
}
sw.Stop();
Console.WriteLine(" Time={0:N0}ms", sw.ElapsedMilliseconds);
}
//挑選三個抽樣檢查執行結果是否一致
Console.WriteLine("{0} {1} {2}",
toFill[1].Code, toFill[10].Code, toFill[50].Code);
Console.Read();
}
static string GetCode1(int n, int sn)
{
return pool.Single(o => o.No == n && o.SubNo == sn).Code;
}
static string GetCode2(int n, int sn)
{
return shuffled.Single(o => o.No == n && o.SubNo == sn).Code;
}
static string GetCode3(int n, int sn)
{
return dict[string.Format("{0}\t{1}", n, sn)].Code;
}
static string GetCode4(int n, int sn)
{
return strDict[string.Format("{0}\t{1}", n, sn)];
}
}
class Boo
{
public int No;
public int SubNo;
public string Code;
}
}