我有列表有150K元素。工作的平均時間IndexOf()比Contains()低4倍。我試圖使用int列表。對於字符串列表IndexOf有點快。爲什麼列表<T> .IndexOf()快了很多比列表<T>。載有()?
我發現只有一個主要的區別,它的屬性TargetedPatchingOptOut。 MSDN告訴:
指示該屬性所應用到的.net框架類文庫的方法是不可能被釋放服務的影響,並且因此可享有橫跨機映像生成器(NGEN)圖像被內聯。
難道這個屬性是這種行爲的原因是什麼?爲什麼Contains()方法沒有這樣的屬性?
在此先感謝。
編輯:
我的代碼是這樣的:
List<int> list = CommonHelper.GetRandomList(size);
long min = long.MaxValue;
long max = 0;
long sum = 0;
foreach (var i in list)
{
m_stopwatch.Reset();
m_stopwatch.Start();
list.Contains(i); // list.IndexOf(i);
m_stopwatch.Stop();
long ticks = m_stopwatch.ElapsedTicks;
if (ticks < min)
min = ticks;
if (ticks > max)
max = ticks;
sum += ticks;
}
long averageSum = sum/size;
編輯2:
我已經寫了相同如IndexOf()中的代碼,並且它比Contains()的工作速度慢。
什麼是在這種情況下,數據的? – 2010-10-28 05:14:12
而且不 - 我不認爲這個屬性與它有任何關係。 – 2010-10-28 05:15:00
我使用int和string,行爲是一樣的。 – 2010-10-28 05:16:26