Hello,
I have List which has 150K elements. Average time of work IndexOf() is 4 times lower than Contains(). I tried to use List of int. For List of strings IndexOf is a bit faster.
I found only one main difference, it's attribute TargetedPatchingOptOut. MSDN tells:
Indicates that the .NET Framework class library method to which this attribute is applied is unlikely to be affected by servicing releases, and therefore is eligible to be inlined across Native Image Generator (NGen) images.
Could this attribute be a reason of such behavior? And why doesn't method Contains() have such attribute?
Thanks in advance.
EDIT:
I have code something like this:
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;
EDIT 2:
I have written same code as in IndexOf() and it work slower than Contains().