Optimizing questions
#1

I already asked this in "general minor question's", but didn't get well questions, so I'll ask it here again.

Quote:

Message originally posted by TehEbil on 04/01/2011 at 03:54 PM:

Hey, atm I'm optimizing my script, so I've got some questions:

1)
Since I know that using of functions make the script slower, I write the part of the function into the command itself, but when functions are useful? If the functions should be repeated more times? And then, it is rly better or just cleaner ?


2)
What is better and what is faster? Or What you would prefer using?
pawn Код:
new sib;

stock function()
{
     sib = ...
}
or

pawn Код:
stock function()
{
     new sib = ...
}
For sure, the second looks cleaner, but in this case, we always create a new variable instead of using the global one, so I don't understand why people do not use just one string[127] for all their [127] strings instead of always creating new one's in every 10th line... And what about static?

3)
solved

4)
Since functions are slower than defines, I try to convert my functions into defines, but there's my first error:

pawn Код:
#define AdminLog(%0,%1,%2,%3)   getdate(year2, month2, day2);   gettime(hour2, minute2, second2); format(Query, 235, "INSERT INTO `Admin Logs` (`Adminname`, `Spielername`, `Aktion`, `Grund`, `Datum`) VALUES('%s', '%s', '%s', '%s', '%d-%d-%d %d:%d:%d')", pInfo[%0][Name], pInfo[%1][Name], %3, %4, year2, month2, day2, hour2, minute2, second2); mysql_query(Query);
                               
tried this this way and:                               

#define AdminLog(%0,%1,%2,%3)   getdate(year2, month2, day2);
                                gettime(hour2, minute2, second2); \
                                format(Query, 235, "INSERT INTO `Admin Logs` (`Adminname`, `Spielername`, `Aktion`, `Grund`, `Datum`) VALUES('%s', '%s', '%s', '%s', '%d-%d-%d %d:%d:%d')", pInfo[%0][Name], pInfo[%1][Name], %3, %4, year2, month2, day2, hour2, minute2, second2); \
                                mysql_query(Query);
but same error:
warning 236: unknown parameter in substitution (incorrect #define pattern)

Line:
AdminLog(playerid, ID, "Kick", string);


5) Last question:
Is it a different between using new string[120] and string[127]? I think so, but I read somewhere that you should take the 2І, 2і, ... (-1, cause of \0) parts, is that true?

- TehEbil

edit:

6)
What is 'better'?

pawn Код:
if()
   { ... return 1; }
...

or

if()
{ ... return 1; }
else
{
...
}

or

if()
   ...
else
{
...
}

Example:

COMMAND:o(playerid, params[])
{
    IsAdmin(==0, playerid)
    new string[127];
    if(sscanf(params, "s[MAX_STRINGS]", string)) return SendClientMessage(playerid, FARBE_WEISS, "Benutze: /o [Text]");
    SendFormattedTextToAll(FARBE_WEISS, "(( %s: %s ))",pInfo[playerid][Name], string);
    return 1;
}
I read somewhere that there shall be an 'else' because the compiler does ( not ) something with it

- TehEbil

Reply
#2

You must decide what to optimize - Speed or size.
Reply
#3

ima answer this quickly before i go to bed (excuse any typos).


1). A function call isnt that bad, you should just avoid unneeded calls when you can (why call something multiple times when you dont have to after all). Functions are useful when a bit of code is called a lot, and is pretty big in size. Instead of copying and pasting a ton of code that clutters your script, you just call a function.

2). It depends on what the use of the variable is, generally if something isnt global you shouldnt declare it as such (scope CAN be very important, sometimes you dont want a part of the script to have access to a var). There is no speed difference (even if there was, it would be very insignificant). If the question is asking if you should declare all vars as global and reuse them, you can as long as you are careful (pawn is single threaded, so it can only do one thing at a time, but you still have to watch out for variables getting modded when you dont want them to be). Memory isnt really a big deal in this day and age, so dont be affraid of tmp or local variables (speed is what matters here). I personaly use 2 global strings (with huge sizes for general purpose things like queries) you can do that too just make sure you null them when you need to.

3). glad its solved.

4). Its because your using variables that come out of now where at all, you need to be using the params you put (%0,%1,%2, and %3). Also dont end macro's with a semicolon, or the usage will be funny (ie: AdminLog(...) with no semicolon).

5). Dont get this question really. String size doesnt matter at all, just make sure you have room for the information you are storing AND one extra space for the terminating NULL character (\0). Otherwise when the NULL character is appending, it will be out of the arrays boundaries.

6). You said 5 was the last question, liez!!! lol, it really depends on preference. I tend to just use if/else block statements as apposed to single lined statements, else if, etc etc.


Edit:

Just saw the last line of #6, and thats not true. The code will run fine (excluding your syntax errors, and just judging the structure), if it the sscanf statement returns true (this means the parse failed) it will return control back to the program, if not it will simply continue to your SendFormattedTextToAll function and then return once again.
Reply
#4

Well, especially speed, but a mix of both would fit best.

edit:
ups, sent it later than i actually wrote it, now first reading your post^^
Reply
#5

I always use global temp variables, I've managed to keep my scripts under 50kb in compile size when doing so
Reply
#6

4) well, i declared those vars global on top of the script, so it should work, like my format function.
5) Sure they don't? As I know they take more space if they are bigger. So the question is: Is string[127] bigger than string[120] ? For the first view I'd say yes, but in the RAM, I read/understood they use degree's of 2, so 128.
Maybe it's clearer with the following question: string[127] and string[128], is the difference high? Means: It's just 1 byte or 129 byte untill the next degree of 2nd is reached?

6)
Okay, that's nice to hear.

To your edit:
A ZCMD-function should return 0 if it success?


For sure, I look forward to hearing more different oppinions

edit:
damn, didn't wanted to post twice, wanted to edit in my other post but Іlate.

@Silent
Well, but I think its senseless to create 1000 times a string, if just one would do, too. For sure, it depends on which it is used for ( for chat ~ 127, for commands just 16 or sth. like that ).

ups, mixxed up global with local, so nvm ^^
Reply
#7

Quote:
Originally Posted by TehEbil
Посмотреть сообщение
4) well, i declared those vars global on top of the script, so it should work, like my format function.
5) Sure they don't? As I know they take more space if they are bigger. So the question is: Is string[127] bigger than string[120] ? For the first view I'd say yes, but in the RAM, I read/understood they use degree's of 2, so 128.
Maybe it's clearer with the following question: string[127] and string[128], is the difference high? Means: It's just 1 byte or 129 byte untill the next degree of 2nd is reached?

6)
Okay, that's nice to hear.

To your edit:
A ZCMD-function should return 0 if it success?


For sure, I look forward to hearing more different oppinions

edit:
damn, didn't wanted to post twice, wanted to edit in my other post but Іlate.

@Silent
Well, but I think its senseless to create 1000 times a string, if just one would do, too. For sure, it depends on which it is used for ( for chat ~ 127, for commands just 16 or sth. like that ).

ups, mixxed up global with local, so nvm ^^
4). You can do that, its fine to use global variables, just be careful :P.

5). They are bigger in size yes. The bigger the array is, the more memory you use. This has nothing to do with powers of two (it just so happens 128 and 256 are common sizes). You can get the number of bites an array uses by multiplying all of the number of cells by 4. So "new string[500];" would take roughly 2kb of ram (500*4), which is nothing lol, and "new string[500][500];" would take roughly 1mb of ram ((500*500) * 4).

6). I dont know off the top of my head, i dont use zcmd lol. You'd have to check in the topic.
Reply
#8

4) well, i just wanted to explain it ^^ It still does not work, just was a reply to "Its because your using variables that come out of now where at all".

5) Okay thanks, but why I need to multiple it with 4? As I know, 1 byte = 1 Char and not 4byte = 1 Char. If you create a .txt file with exactly 1000 chars, you have got a filesize of 1kb. Does Pawn need more? Or do you mean bits? And then, why it is 4 and not 7 or 8 (ansii)

6) Ok, I'll do, thanks.
Reply
#9

Well pawn works with 4byte sized variables to allow for numbers up to 0xFFFFFFFF. There should be an option to set a variables' size
Reply
#10

There is, and It's called packed strings. An example is
pawn Код:
new var[4 char];
Read the pawn documentation for more info
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)