Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further optimizations #5

Open
izrie1 opened this issue May 5, 2020 · 1 comment
Open

Further optimizations #5

izrie1 opened this issue May 5, 2020 · 1 comment

Comments

@izrie1
Copy link

izrie1 commented May 5, 2020

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

@uPeppe
Copy link
Owner

uPeppe commented May 5, 2020

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;

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants