简体   繁体   中英

How is List<T>.IndexOf() implemented in C#?

I was thinking about the performance of calling List<T>.Indexof(item) . I am not sure if it will be a O(n) performance for a sequential algorithm or O(log(n)) performance for a binary tree??

Using Reflector for .NET we can see:

public int IndexOf(T item)
{
    return Array.IndexOf<T>(this._items, item, 0, this._size);
}

public static int IndexOf<T>(T[] array, T value, int startIndex, int count)
{
    return EqualityComparer<T>.Default.IndexOf(array, value, startIndex, count);
}

internal virtual int IndexOf(T[] array, T value, int startIndex, int count)
{
    int num = startIndex + count;
    for (int i = startIndex; i < num; i++)
    {
        if (this.Equals(array[i], value))
            return i;
    }
    return -1;
}

It's O(n) according to MSDN .

This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.

List<T>由平面数组支持,因此list.IndexOf(item)为O(n)。

List<T>.IndexOf is O(n) which is in fact optimal for an unordered set of n elements.

List<T>.BinarySearch is O(log n) but only works correctly if the List is sorted.

If you need a faster performer, consider HashSet<T> . It's a speed vs. memory tradeoff, but it is worth it if you value the former over the latter.

(It's not exactly the same as a List<T> , it behaves like a single column dictionary, but for instances where you are going to have a unique list, it's one way to do it.)

My late answer but I think it worth mentioning that nowadays you can directly access to the MS sources: http://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs

No more need for reflection since the .NET BCL code is now available online.

Implements a variable-size List that uses an array of objects to store the elements. A List has a capacity, which is the allocated length of the internal array. As elements are added to a List, the capacity of the List is automatically increased as required by reallocating the internal array.

As implemented as an array and performing a linear search , you can easily deduce that the algorithmic complexity of the IndexOf method is O(n) .

As mentioned by others the information are publicly available on the msdn: https://msdn.microsoft.com/en-us/library/e4w08k17(v=vs.110).aspx

This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.

Again, you can check out the sources and end up seing that the static helper method IndexOf of the Array class is actually called behind the scenes:

http://referencesource.microsoft.com/#mscorlib/system/array.cs

If the list / array is already sorted beforehand you can then rather use a binary search: https://msdn.microsoft.com/en-us/library/w4e7fxsh(v=vs.110).aspx

This method is an O(log n) operation, where n is the number of elements in the range.

Behind the scenes a regular array is used, infact the IndexOf method simply calls Array.IndexOf . Since arrays don't sort elements, performance is O(n).

这是一个老问题,但我认为这个链接很有趣: 实验:添加新元素时列出内部和性能

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM