Raised This Month: $ Target: $400
 0% 

Optimizing Plugins


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Dr7sTyLe
Senior Member
Join Date: Dec 2010
Old 11-05-2011 , 15:33   Optimizing Plugins
Reply With Quote #1

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.
Dr7sTyLe is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-05-2011 , 15:35   Re: Optimizing Plugins
Reply With Quote #2

What don't you understand? It just says to not declare an array in a loop.
__________________
fysiks is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 11-05-2011 , 17:06   Re: Optimizing Plugins
Reply With Quote #3

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.

Last edited by Emp`; 11-05-2011 at 17:07.
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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