Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiency of List<T>.IndexOf() versus List<T>.FindIndex()

Which one of the methods

  • List<T>.IndexOf() and
  • List<T>.FindIndex()

is more efficient in terms of processing time?

The type of T in this instance is String.

like image 278
user3409181 Avatar asked Aug 12 '15 12:08

user3409181


People also ask

What is the difference between FindIndex and indexOf?

findIndex - Returns the index of the first element in the array where predicate is true, and -1 otherwise. indexOf - Returns the index of the first occurrence of a value in an array.

What is FindIndex in c#?

FindIndex Method is used to search for an element that matches the conditions defined by a specified predicate and returns the index of the first occurrence within the List<T>. If an item which matches the conditions is not found then this method will return -1.

How to find list index in c#?

To get the index of an item in a single line, use the FindIndex() and Contains() method. int index = myList.

Is indexOf slow?

EDIT: Based on more tests, indexOf seems to run faster than a for loop in the version of Safari I'm using (5.0. 3) and slower in just about everything else.


2 Answers

IndexOf performs a for-loop, using the Equals implementation of the objects being searched to look for a match. FindIndex also peforms a for-loop but evaluates a Predicate to check for a match instead.

They each boil down to a for-loop. While they both technically have an O(n) design, the use of a delegate in FindIndex will have some overhead. The difference in performance can be seen in Denis19901's answer. Here are some MSDN excerpts:

List<T>.IndexOf Method (T):

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

List<T>.FindIndex Method (Predicate<T>):

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

That said, the two functions would be used quite differently. The former assumes you have an object from the list, and you just need to know at what index it exists at (if any) in the list.

The latter assumes you know some criteria about an object, and you want to find the first index where an object in the list matches that criteria. There could be multiple matches, but the method returns the first match.

like image 177
Cᴏʀʏ Avatar answered Sep 28 '22 08:09

Cᴏʀʏ


Cᴏʀʏ suggested that the difference in performance would be negligible, if any at all. And since my intuition said that using a delegate is always going to be slower, I decided to put it to the test.

Using DotNetBenchMark and the following code to test:

[MemoryDiagnoser]
public class Bench
{
    byte[] buffer;

    public Bench()
    {
        buffer = new byte[1024];
    }

    [Benchmark]
    public void FindIndex()
    {
        int index = Array.FindIndex(buffer, x => x == byte.MaxValue);
    }

    [Benchmark]
    public void IndexOf()
    {
        int index = Array.IndexOf(buffer, byte.MaxValue);
    }
}
  • FindIndex had a mean runtime of 1,986.2 ns
  • IndexOf had a mean runtime of 178.4 ns

Doesn't seem like the difference in performance is negligible, as it is more than 10 times slower.

And even in a case where the 16th element of the array matches, IndexOf is still more than twice as fast as FindIndex

like image 37
Dennis19901 Avatar answered Sep 28 '22 07:09

Dennis19901