Raised This Month: $ Target: $400
 0% 

[EXAMPLE] Optimizing scripts when handling strings or arrays


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
SirLamer
Senior Member
Join Date: Oct 2008
Old 01-12-2009 , 12:33   Re: [EXAMPLE] Optimizing scripts when handling strings or arrays
Reply With Quote #8

It's true that in many contexts there isn't much justification in optimizing, there is never a reason to not optimize, so long as the code remains reasonably easy to understand. So just do it!

Optimization is much more important within looping scripts or scripts that create a session for each client all at once or something like that, but complex structures like these make for very poor and confusing example programs. This simple example could easily be applied to more broad contexts. But remember, I was mistaken in not knowing that the function sizeof() is a compiler directive, hence my surprise to see rampant use all over many programs. For other cases, remember that a function called within the header of a for loop will run not just once but one time for every iteration. So, a 200-cycle loop will call the function 200 times!

Also consider that most game servers are rented and share several machines, which severely restricts available CPU and memory resources as compared to a dedicated or privately owned server machine. If all scripts are CPU and memory-selfish in their design, not considering that they run among several other plugins and even within several other game servers on the same machine, things can get muddled.

I put the #define there just to clearly demonstrate the association as an example, however I disagree with the practice of jamming all the #define parameters at the top of the script as it is much harder then to track relationships should the need arise to edit scripts or move functions around between separate plugins or whatever. I put #define calls up top only when the value is required in more than one place, and otherwise I stack the #define calls at the top of each function. As far as the compiler is concerned, it doesn't matter where the #define calls are as long as they appear in the code before they are put to use (ie. they are not subject to scope). In almost all cases using #define and then applying the parameter name throughout your code is preferred over just manually typing in the values everywhere because it permits rapid refinement and it clearly defines value relationships, which is more more intricate in SourcePawn than in, say, C++ or PHP. For example, defining array lengths with #define parameters avoids the risk of a programmer changing an array length without changing the associated reported "maxlength" in the various functions it is used with.

Last edited by SirLamer; 01-12-2009 at 12:36.
SirLamer is offline
 



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 18:46.


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