How fast is "System.Array.IndexOf" on iPhone?

hi there!

I was wondering what would be faster, using for(...) or foreach(...) on arrays in C# on iPhone like

foreach (GameObject foo in bArray) int index = System.Array.IndexOf(bArray, foo);

or

for(int i=0; i<= bArray.length-1; i++) int index = i;

thnx!

Your first example makes little sense, so the second one

IndexOf should be relatively fast, but it's not for getting the count in a loop - use a count variable for that and increment it each time

Regarding for vs foreach - it depends.

Usually you'll want to use whichever is easiest to read, there's very little (if any) difference in speed for native arrays

A for loop is faster and should be used when you want to make use of the index. Any index searching functions just add overhead.

for(int i=0; i < bArray.length; i++) 
    int index = i;

IndexOf need to walk through the array so your time complexity would be squared. So for large lists it will become slower (Well I'd be surprised if the foreach ever would be faster)

foreach (GameObject foo in bArray) 
    int index = System.Array.IndexOf(bArray, foo); // <- for/each inside

// basically the same as:

foreach (GameObject foo in bArray) 
{
    int index = -1;
    for (int i = 0; i < bArray.length; ++i)
    {
        if (bArray *== foo)*
 *{*
 *index = i;*
 *break;*
 *}* 
 *}*
*}*
*```*
*<p>So you can imagine it is quite slower.</p>*