Array.IndexOf performance caveat[Gang Peng]

Array.IndexOf performance caveat[Gang Peng]

  • Comments 1

Array.IndexOf are “generic” methods to search for an item in an one dimensional array. However since they are generic, there are some performance caveats.

 

Array.IndexOf handles arrays in three different ways:

(1)    If the array is a SZArray (one dimensional array with zero-based indexing) and element type is one of following types: Byte, SByte, Boolean, Int16, UInt16, Char, Int32, UInt32, Int64, UInt64, Single, Double, the search will be done in unmanaged code. This will be pretty fast. Note we do need to unbox the value once.

(2)    If the array is a SZArray of reference type, we can Object.Equals on each value in the array until we find a matching item. This is not bad either.

(3)    When the array is a SZArray of other value types or the lower bound is not zero, the code is like:

for (int i = startIndex; i < endIndex; i++) {

Object obj = array.GetValue(i);

if (value.Equals(obj)) return i;

}    

GetValue is quite costly and the worst part is it could cause boxing for each items in the array we searched for. I wrote a simple test to search for an item in an Array of a simple custom value type. Using for loop to search for an itemsin the Array is about 40x faster than calling Array.IndexOf.

 

Generics comes to rescue here. The performance problem doesn’t exist for SZArray on Whidbey. C# compiler favors IndexOf<T>. So if you recompile your code on Whidbey, you could get rid of the performance problem for free. (Note the generic version is actually slower than the non-generic version under some cases on Whidbey Beta1 build. We will fix that before Whidbey ships.)

 

There are two other things worth mentioning:

(1) Array.IndexOf doesn’t support conversion between primitive types. Following code will print out -1.

long[] array = new long[10];

       for( int i = 0; i < 10; i++)  { array[i] = i; }

       Console.WriteLine(Array.IndexOf(array, 9));

 

(2) The item to be searched is passed in as Object. So there is a boxing operation when IndexOf is called in following code:

       int[] array = new int[10];

      

       Array.IndexOf(array, 9);

This problem is solved by Generics. One workaround for this on V1 and V1.1 is to use pre-boxed objects.

 

For details, you can take a look at Rotor source:

 

http://dotnet.di.unipi.it/Content/sscli/docs/doxygen/fx/bcl/classSystem_1_1Array.html#f8

http://dotnet.di.unipi.it/Content/sscli/docs/doxygen/clr/vm/comarrayhelpers_8cpp.html#a0