Floats - Do you use the natives for calculating or the operators?
#1

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
Reply
#2

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;
Reply
#3

Quote:
Originally Posted by X337
View Post
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
Reply
#4

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.
Reply
#5

Quote:
Originally Posted by Vince
View Post
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 0count < %1count++)\
    {\
        %
0;\
    }\
    
printf("%s: %dms in %d iterations", %2GetTickCount() - start, %1);\
    }
#define TEST_ITERATIONS 10000000
BENCHMARK((1000.0 150.0) / 7.5TEST_ITERATIONS"operators - and /");
BENCHMARK(floatdiv(floatsub(1000.0150.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.
Reply
#6

Because operators wouldn't work without natives, take a look in float.inc.
Reply
#7

Quote:
Originally Posted by Spmn
View Post
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?
Reply
#8

Personally I prefer operators, easy to understand!
Reply
#9

It depends on what you are going to do
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)