Array.indexof performance problem

xiaoxiao2021-03-06  67

Array.Indexo is a "generic" method that searches for specified objects in a one-dimensional array and returns the first match index, because it is generic, so there is a number of performances to pay attention to. Array.IndexoF operators have three different situations: UINT64, SINGLE, DOUBLE, the query will be performed under the unmanaged code, the speed back is very fast, pay attention, and the value is removed back to each of the values. 2. If the elements in the array are reference types, we can use the Object.Equal method to compare each element in the array until we find the matching element. 3. When the element in the array is other value type, or the lower limit is not 0, then look for the following code: for (int i = startIndex; i

Object obj = array.getvalue (i);

Value.equals (obj)) RETURN I;

} The cost using the getValue method is very high, and it also has the value of each of our findings. I have done a simple test, in an array of custom types, at this time, use the loop in the array than to use Array.IndexOf to be nearly 40 times faster.

There are two worthless questions:

1.array.indexof does not support conversion between basic types, in the following example, will output -1

Long [] List = new long [10];

For (int i = 0; i <10; i )

{List [i] = i;}

Console.writeline (Array.indexof (List, 9));

转载请注明原文地址:https://www.9cbs.com/read-89783.html

New Post(0)