How can I turn this to #define
#1

Hey guys, If im not wrong there's a way which I can turn this from stock to define, so I can save memory
Can you tell me how?
Please try to explain because basically I am supposed to learn something
Thanks in advice
PHP Code:
stock IsAuth(playeridalevel)
{
    if(
aInfo[playerid][aLogged] && aInfo[playerid][aLevel] >= alevel) return 1;
    else return 
0;

Reply
#2

I don't see a point in doing so because declaring a function is just minimal overhead, but:

Code:
#define IsAuth(%1,%2) (aInfo[%1][aLogged]&&aInfo[%1][aLevel]>=%2)
You successfully saved memory worth 2 bytes or so.

%1 and %2 are placeholders, so anything you put in will then replace %1 and %2 within the code.
Reply
#3

Quote:
Originally Posted by NaS
View Post
I don't see a point in doing so because it's minimal code, but:

Code:
#define IsAuth(%1,%2) (aInfo[%1][aLogged]&&aInfo[%1][aLevel]>=%2)
You successfully saved memory worth 2 bytes or so.
Well I also agree with you, although my friend has tipped me to use as less stocks as I can ...
Reply
#4

This is stupid and unnecessary. You'll only increase the size of the compiled amx and therefore you'll also consume more memory, not less. A define or macro merely does some basic text replacing, which means the code is effectively copied to all places that it used, rather than being referenced from a single location.

Furthermore, there's no such thing as "a stock". It's either "a function" or "a stock function" but never just "a stock". A stock function should - as its name implies - contain stock code that can be used in any and all scripts without changes. This function relies on global variables and should therefore not be declared stock.
Reply
#5

Quote:
Originally Posted by NeXoR
View Post
Well I also agree with you, although my friend has tipped me to use as less stocks as I can ...
Then just remove "stock" from the declaration.
Using less stock function "prefixes" is mostly an aesthetic thing, there is no actual use from it. They are meant for includes, because you don't want 200 warnings about unused functions in your script.

So that advice does not really fit here, just saying.

In the end it doesn't really matter if it's a stock function or not, but if you want to do it correctly you should know when to use stock and where it's simply pointless.
Reply
#6

Quote:
Originally Posted by Vince
View Post
This is stupid and unnecessary. You'll only increase the size of the compiled amx and therefore you'll also consume more memory, not less. A define or macro merely does some basic text replacing, which means the code is effectively copied to all places that it used, rather than being referenced from a single location.

Furthermore, there's no such thing as "a stock". It's either "a function" or "a stock function" but never just "a stock". A stock function should - as its name implies - contain stock code that can be used in any and all scripts without changes. This function relies on global variables and should therefore not be declared stock.
Isn't direct logical calculation/analysis like WAY WAY faster than calling a function in pawn?
welp if it isn't I kinda learned a new thing today!
Although FYI I'm more concerned about CPU than Ram tho, I would sacrifice a couple bytes, hell even megabytes to speed any day.
Reply
#7

Quote:
Originally Posted by Vince
View Post
This is stupid and unnecessary. You'll only increase the size of the compiled amx and therefore you'll also consume more memory, not less. A define or macro merely does some basic text replacing, which means the code is effectively copied to all places that it used, rather than being referenced from a single location.

Furthermore, there's no such thing as "a stock". It's either "a function" or "a stock function" but never just "a stock". A stock function should - as its name implies - contain stock code that can be used in any and all scripts without changes. This function relies on global variables and should therefore not be declared stock.
Thank you, learned something new today
I didn't really know how the compiler works, but now I got the message.
Thank you, once again.
P.S you shouldn't use "stupid" xD
Remember that we all have started as newbies
Reply
#8

Quote:
Originally Posted by PrO.GameR
View Post
Isn't direct logical calculation/analysis like WAY WAY faster than calling a function in pawn?
welp if it isn't I kinda learned a new thing today!
Although FYI I'm more concerned about CPU than Ram tho, I would sacrifice a couple bytes, hell even megabytes to speed any day.
You're partially right, I'd totally prefer CPU saving over RAM savings too, but "WAY WAY" faster is wrong.
There is no noticable difference, I bet you couldn't even measure the difference within 100000 calls.
Calling any native function is taking way more time than the overhead for using a function instead of direct code.

It's basically just a jump to another address and filling some registers, instead of directly calling the desired code.
You shouldn't really worry about this tiny bit of overhead when trying to optimize.

Furthermore defines have their limits, sure you can convert some small codes but a complex function can not be done with a define. And if you could, it's not worth the time doing so.

Quote:

P.S you shouldn't use "stupid" xD

He should, because that is correct. He didn't call you stupid, so I don't see a problem here
Reply
#9

I actually once tested how much difference in speed there is.
It was about 10000000 calls (Can't remember the exact number, but either remove or add a zero)
And the define was only 1 ms faster, so basically it's the same thing, except that functions use less memory.
Reply
#10

So after all the answers I got curious, really wanted to know if it would make a difference, and it actually does!

here is a benchmark test, defining is almost 2.5x faster !
PHP Code:
1 million iterations
Benchmarks
define57 ms
Benchmarks
: function call142 ms 
Here is the code if you want to check it yourself

PHP Code:
#define FILTERSCRIPT
#include <a_samp>
#define FUNC1(%0,%1) (Benchtest[%0]==%1)
new Benchtest[1000000];
public 
OnFilterScriptInit()
{
    new 
time[3];
    
Benchtest[999999]=100;
    
time[0]=GetTickCount();
    for(new 
i;i<1000000;i++)
    {
        if(
FUNC1(i,100)) print("found one!");
    }
    
time[1]=GetTickCount();
    for(new 
i;i<1000000;i++)
    {
        if(
FUNC2(i,100)) print("found one!");
    }
    
time[2]=GetTickCount();
    print(
"1 million iterations benchmark test");
    
printf("Benchmarks: define: %i ms",time[1]-time[0]);
    
printf("Benchmarks: function call: %i ms",time[2]-time[1]);
}
FUNC2(index,value)
{
    return (
Benchtest[index]==value);

This is for simple stuff like IsPlayerCuffed, stuff like that which simply check a value, technically for anything other than a 1 line function define shouldn't be used.
Would love to hear more experienced ppl's opinion on know if I did something wrong or did I prove it right, I know I know this doesn't cover AMX file growth, but srsly with 8GB of ram on every pc nowadays a couple (even hundred) megabytes is nothing.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)