Array.IndexOf performance caveat

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值