Floats - Do you use the natives for calculating or the operators? -
BiosMarcel - 14.01.2017
Hey,
So, this is just something i am curious about, how do you usually calculate, with natives (floatsub, floatadd, floatdiv, floatmul) or using operators (-, +, /, *)? Leave a comment on why you use which one.
As for me, i have always used the operators, even though the are slower than the natives, since i never really thought the impact could be big. But, i think that i'll change my mind and start using the natives, at least in some cases, in loops with expensive calculations for example.
greetings Marcel
Re: Floats - Do you use the natives for calculating or the operators? -
X337 - 14.01.2017
I'm using operators, and natives sometimes.
I think there's no difference between them. Because afaik float operators are using that native functions too.
Code:
native Float:operator+(Float:oper1, Float:oper2) = floatadd;
Re: Floats - Do you use the natives for calculating or the operators? -
BiosMarcel - 14.01.2017
Quote:
Originally Posted by X337
I think there's no difference between them. Because afaik float operators are using that native functions too.
Code:
native Float:operator+(Float:oper1, Float:oper2) = floatadd;
|
I have done some benchmarking and there definitly was a difference
The natives were 18-20% faster.
EDIT:
Not quite right:
http://forum.sa-mp.com/showpost.php?...16&postcount=5
Re: Floats - Do you use the natives for calculating or the operators? -
Vince - 14.01.2017
I would very much like to see that benchmark because that seems a lot. I use the operators purely for aesthetic purposes and readability. Especially in longer calculations where the oversight would be easily lost due to the overabundance of brackets. That old GetDistanceBetweenPlayers function that people keep copying is a prime example.
Re: Floats - Do you use the natives for calculating or the operators? -
BiosMarcel - 14.01.2017
Quote:
Originally Posted by Vince
I would very much like to see that benchmark because that seems a lot. I use the operators purely for aesthetic purposes and readability. Especially in longer calculations where the oversight would be easily lost due to the overabundance of brackets. That old GetDistanceBetweenPlayers function that people keep copying is a prime example.
|
The thing i tested was this:
PHP Code:
#define BENCHMARK(%0,%1,%2); \
{\
new start = GetTickCount();\
for(new count = 0; count < %1; count++)\
{\
%0;\
}\
printf("%s: %dms in %d iterations", %2, GetTickCount() - start, %1);\
}
#define TEST_ITERATIONS 10000000
BENCHMARK((1000.0 - 150.0) / 7.5, TEST_ITERATIONS, "operators - and /");
BENCHMARK(floatdiv(floatsub(1000.0, 150.0), 7.5), TEST_ITERATIONS, "natives floatsub and floatdiv");
Ok, i changed this a little, but still the difference is about 10%
And yeah, i just realized how retarded that was, that i just generalized that the natives are faster, even though i didn't test them all and i just tested a specific case. in this case for example it is faster, but there were other cases where it was equally fast.
Maybe i am just too dumb to do benchmarking, that might be the case
Sorry for doing the thinking after the doing ...
But anyways, if the natives aren't faster, why would they exist, i have asked myself that.
Re: Floats - Do you use the natives for calculating or the operators? -
Spmn - 14.01.2017
Because operators wouldn't work without natives, take a look in float.inc.
Re: Floats - Do you use the natives for calculating or the operators? -
BiosMarcel - 14.01.2017
Quote:
Originally Posted by Spmn
Because operators wouldn't work without natives, take a look in float.inc.
|
Ah, so the operators are just some kind of makros or did i get it wrong?
Re: Floats - Do you use the natives for calculating or the operators? -
YouHack - 14.01.2017
Personally I prefer operators, easy to understand!
Respuesta: Floats - Do you use the natives for calculating or the operators? -
Whyd - 14.01.2017
It depends on what you are going to do