Further optimizations
izrie1 opened this issue · comments
Codes like this could be optimized significantly using VectorSize
Codes below seems to be very slow according to performance profiler results..
stock IsObjectInSphere(objectid,Float:x,Float:y,Float:z,Float:radius2) { new Float:x1,Float:y1,Float:z1,Float:tmpdis; GetObjectPos(objectid,x1,y1,z1); tmpdis = floatsqroot(floatpower(floatabs(floatsub(x,x1)),2)+ floatpower(floatabs(floatsub(y,y1)),2)+ floatpower(floatabs(floatsub(z,z1)),2)); // if(tmpdis < radius2) return 1; return 0; }
to
`IsObjectInSphere(objectid, Float:x, Float:y, Float:z, Float:radius2)
{
new Float:x1, Float:y1, Float:z1;
if (GetObjectPos(objectid, x1, y1, z1)) // object exists
{
return VectorSize(x1-x, y1-y, z1-z) <= radius2;
}
return 0;
}`
a more optimized version
Perhaps you could consider doing major optimizations on the scripts so it performs better
Thanks for the tip! I didn't know about the VectorSize function introduced in last SA-MP builds
I think that "IsObjectInSphere" function is slow because it is written very badly (there's no need of using floatsqroot, floatabs and floatpower functions). Ideally it should be written like this:
`IsObjectInSphere(objectid, Float:x, Float:y, Float:z, Float:radius2)
{
new Float:x1, Float:y1, Float:z1;
if (GetObjectPos(objectid, x1, y1, z1))
{
x1 -= x;
y1 -= y;
z1 -= z;
return (x1 * x1 + y1 * y1 + z1 * z1) < (radius2 * radius2);
}
return 0;
}`