Performance Question: function parameters vs points and multiplication vs checks

Hi, I couldn’t find the answer to this and I’m hoping someone knows off the top of their head.

I heard that pointers are kind of slow on iPhone… but I don’t know if passing parameters would be faster (unless I suppose they are used many times).

I want to know if it is more efficient (in iPhone development) to pass parameters from a object to a function… or to reference the object’s variables through a pointer from the method.

Also I want to know whether doing a radius check, which would allow for fewer calculations, would be faster than doing a nested if with single dimensional checks… eg:

Doing a couple single dimension checks first to avoid multiplication 99% of the time:

float xDiff = 0;
float zDiff = 0;
float sqrDist = 0;
float minCollisionDistance = 2;
float minSqrCollisionDistance = 4;

for (int i = 0; i < enemies.Count; i++) {       
     xDiff = enemies[i].x - x;
                    
     if (Mathf.Abs(xDiff) < minCollisionDistance) {

           zDiff = enemies[i].z - z;

           if (Mathf.Abs(zDiff) < minCollisionDistance) {
                        
                sqrDist = xDiff * xDiff + zDiff * zDiff;
                           
                if (sqrDist < minSqrCollisionDistance) {                               
                    PerformSomething(); 
                }
          }
     }   
}

or would this be faster to cut out the extra checks:

float xDiff = 0;
float zDiff = 0;
float sqrDist = 0;

for (int i = 0; i < enemies.Count; i++) {       
     xDiff = enemies[i].x - x;
     zDiff = enemies[i].z - z;      
     sqrDist = xDiff * xDiff + zDiff * zDiff;
                           
      if (sqrDist < minSqrCollisionDistance) {                               
            PerformSomething(); 
      }
         
}

The first one will 90 percent of the time not need to check further than the first check ( if (Mathf.Abs(xDiff) < minCollisionDistance) )… but if the Mathf.Abs is about the same speed as the multiplications in the second one then it would be less efficient.

Also… should I be iterating through a List using a “(for int i = 0 ; i < list.Count; i++)” or would it be better to use a iterator.

Also, and maybe this is what I should have asked, is there anywhere good documentation to scripting for the iPhone hardware. I’ve only ever come across bits and pieces.

Generally calling square roots for an early out is bad practise. Simple if (x>y) stuff is usually the way to go… Also enemies.Count could be evaluated every loop. Try stuffing that in a variable which has been pre allocated.

You’re doing a full distance check when you should just check a rectangle which is fastr as you can check x, and if that passes, check y and if that passes finally call your sqr then performsomething. It will discard a lot more and run faster with more objects.

The best thing you can always do is just make a benchmark test and see for yourself. Although it’s probably better to think of higher-level optimizations, such as is this in Update, and could it be less often instead? Like use InvokeRepeating a few times per second or whatever.

–Eric

That’s true, I’ve been lazy about that.

Someone told me today that (for Lists anyway) it will be still faster to use a iterator than a integer for running through the List…

foreach (Enemy enemy in enemies) {  
   // Do Calculations
}

Because that way it doesn’t have to travel through the List each time to access the contents (because Lists are not stored continuously, or so I hear).