所以我想衡量哈希集合,我創建對同一元素在List
的表現性能和代碼爲什麼我的ElapsedMilliseconds在這裏始終爲零?
Stopwatch Watch = new Stopwatch();
long tList = 0, tHset = 0; // ms
foreach (string Str in Copy)
{
// measure time to look up string in ordinary list
Watch.Start();
if (ListVersion.Contains(Str)) { }
Watch.Stop();
tList += Watch.ElapsedMilliseconds;
// now measure time to look up same string in my hash set
Watch.Reset();
Watch.Start();
if (this.Contains(Str)) { }
Watch.Stop();
tHset += Watch.ElapsedMilliseconds;
Watch.Reset();
}
int n = Copy.Count;
Console.WriteLine("Average milliseconds to look up in List: {0}", tList/n);
Console.WriteLine("Average milliseconds to look up in hashset: {0}", tHset/n);
以下塊是outputing 0
兩種。任何想法爲什麼這是?相關文檔:https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx
因爲操作太快,你不應該像這樣測量性能。 –