Speed of functions to check distance
#1

Hi!

I tested some functions to check what the fastest function to check if a point is in a defined distance to another point.

4 Functions (1 is a define)
pawn Code:
//Function 1 (modified PlayerToPoint)
stock IsPosInDistance1(Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2, Float:radius)
{
  new Float:tmpx,Float:tmpy, Float:tmpz;
  tmpx = (x1-x2);
  tmpy = (y1-y2);
  tmpz = (z1-z2);
  if (((tmpx < radius) && (tmpx > -radius)) && ((tmpy < radius) && (tmpy > -radius)) && ((tmpz < radius) && (tmpz > -radius)))
      return true;
  return false;
}

//Function 2
#define IsPosInDistance2(%0,%1,%2,%3,%4,%5,%6) \
    (((%0 - %3) * (%0 - %3)) + ((%1 - %4) * (%1 - %4)) + ((%2 - %5) * (%2 - %5)) <= %6 * %6)
   
//Function 3
stock IsPosInDistance3(Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2, Float:radius)
{
    return (floatsqroot((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2))<radius);
}

//Function 4
stock IsPosInDistance4(Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2, Float:radius)
{
  new Float:tmpx,Float:tmpy, Float:tmpz;
  tmpx = (x1-x2);
  tmpy = (y1-y2);
  tmpz = (z1-z2);
    return (floatsqroot(tmpx*tmpx+tmpy*tmpy+tmpz*tmpz)<radius);
}
Code to check speed:
pawn Code:
#define DISTANCE 500.0

new t=GetTickCount(),var;
for(new i=0;i<1000000;i++)
    var=IsPosInDistance1(float(random(6000)-3000),float(random(6000)-3000),float(random(6000)-3000),float(random(6000)-3000),float(random(6000)-3000),float(random(6000)-3000),DISTANCE);
printf("Time 1: %d",GetTickCount()-t);

t=GetTickCount();
for(new i=0;i<1000000;i++)
    var=IsPosInDistance2(float(random(6000)-3000),float(random(6000)-3000),float(random(6000)-3000),float(random(6000)-3000),float(random(6000)-3000),float(random(6000)-3000),DISTANCE);
printf("Time 2: %d",GetTickCount()-t);

t=GetTickCount();
for(new i=0;i<1000000;i++)
    var=IsPosInDistance3(float(random(6000)-3000),float(random(6000)-3000),float(random(6000)-3000),float(random(6000)-3000),float(random(6000)-3000),float(random(6000)-3000),DISTANCE);
printf("Time 3: %d",GetTickCount()-t);

t=GetTickCount();
for(new i=0;i<1000000;i++)
    var=IsPosInDistance4(float(random(6000)-3000),float(random(6000)-3000),float(random(6000)-3000),float(random(6000)-3000),float(random(6000)-3000),float(random(6000)-3000),DISTANCE);
printf("Time 4: %d",GetTickCount()-t);
Now test results:
Code:
1st place: function 1 [~980 ms]
2nd place: function 4 (WTF? I always thought floatsqroot would be extremly slow o___O) [~980 ms]
3rd place: function 3 (Same here with floatsqroot. WTF?) [~1000 ms]
4th place: function 2 (o___O WTF, too!) [~1400 ms]
So, the function floatsqroot is one of the fastest ways to check the distance? :S
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)