AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Optimizing Plugins (https://forums.alliedmods.net/showthread.php?t=171412)

Dr7sTyLe 11-05-2011 15:33

Optimizing Plugins
 
I read something about optimizing plugins which i couldn't understand

Quote:

Likewise, it is equally important to avoid declaring arrays inside of loops. Consider the following block of code:
for (new i=0; i<num; i++)
{
new message[255], name[32], player
player = players[i]
get_user_name(player, name, 31)
format(message, 254, "Hello, %s", name)
}
If there are 32 players, this loop will actually resize and zero out over 1K of memory thirty two times in a row. Not good! However, on the other hand, it's nice that the message is zeroed out for us each time. Tip: you often only need to zero out the first character of a string. This will make the entire string empty. The code below is much more efficient:
new message[255], name[32], player
for (new i=0; i<num; i++)
{
player = players[i]
name[0] = '^0'
message[0] = '^0'
get_user_name(player, name, 31)
format(message, 254, "Hello, %s", name)
}
This has the effect of safely making the string empty beforehand, as well as largely reducing a lot of run-time overhead.
if somone would explain it with easier english or in another way it would be good.

fysiks 11-05-2011 15:35

Re: Optimizing Plugins
 
What don't you understand? It just says to not declare an array in a loop.

Emp` 11-05-2011 17:06

Re: Optimizing Plugins
 
Don't create arrays or strings within loops.

Just remember that the array/string is not cleared each time, and you might want to clear it.


All times are GMT -4. The time now is 14:15.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.