iosobjective-cloopsoptimization

Optimization of local variables in Objective C loop


I remember many years ago they were teaching us to create local variables outside of loops, e.g.:

SomeVariable* var;
for(int i; i<10; i++)
{
    var = [someArray objectAtIndex:i];
    (...)
}

I would assume that with modern compilers such optimization is done automatically, e.g. in Objective C the code below would be optimized to execute as fast as the code above:

for(int i; i<10; i++)
{
    SomeVariable* var = [someArray objectAtIndex:i];
    (...)
}

Am I right?


Solution

  • for-in loop is optimized to execute as fast.

    Eg:

    for( SomeVariable *var in someArray )
    {
        //do something with var
    }