Better way to optimize this?
#1

I am just wondering if there is a way to optimize this bit of code a little better:
pawn Код:
new msgstr[80 + MAX_PLAYER_NAME];
GetPlayerName(playerid, name, sizeof(name));
format(msgstr, sizeof(msgstr), "-MSG- The username '%s' is already registered. Please log in with your Password",name);
SendClientMessage(playerid, COLOR_YELLOW, msgstr);
SendClientMessage(playerid, COLOR_GREY, "-MSG- Not your account? Then relog with another name");
new diastr[60 + MAX_PLAYER_NAME];
format(diastr, sizeof(diastr), "{FFFF00}Your username: {FFFFFF}%s\n\nEnter your Password to log in",name);
ShowPlayerDialog(playerid, MENU_LOGIN, DIALOG_STYLE_PASSWORD, "Hopes Hills RP Account", diastr, "Login", "Quit");
I am thinking of the variables "msgstr" and "diastr". I am just wondering if it is neccesary to have 2 variables? The 2 formats need variables with different array sizes. Is there a way to use the same variable, and redefine the size after the first use?

Thanks a lot.
Reply
#2

Why do you need to redefine the size? Just use msgstr instead of diastr - it's bigger than diastr so it will be big enough to hold everything.
Reply
#3

Changing the size would actually decrease performance since it would take longer to de-and reallocate an array then to just put data in an array that's a little too large.
Reply
#4

You're over-analyzing optimization WAY too much. If you're not using ridiculously large string sizes, then I wouldn't worry about it.
Reply
#5

I assume such "optimization" was relevant back in the 70s with really simplistic hardware. Newer CPUs have quite a lot interesting optimizations that they carry out on their own. And really, whether you allocate 70 or 128 bytes on the stack is not important in terms of performance as 2KY pointed out. That's just pointless optimization. Also, by counting the characters in your strings to get your array sizes to match, you may end up with an buffer overflow.
There are certain aspects to know for fun (if you ever work with code that needs to be really quick, why not try them out), for example allocating and operating on an array the size of which is a power of two may be faster than operating on one with a different size.

What's important in your SA-MP server is to deliver features and keep your players satisfied. You're not in the 80s nor flying a rocket to the Moon. This does not, however, mean that you should write stupid code (thanks to ****** for the article link)! Just don't get too much into micro-optimization.

However the code of yours has 2 strings when it could use just one. So make use of one as many times as you can.
Reply
#6

Thank you
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)