Raised This Month: $51 Target: $400
 12% 

Optimization question: which code is better?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Tauphi
SourceMod Donor
Join Date: Sep 2004
Location: Germany
Old 09-19-2008 , 10:58   Optimization question: which code is better?
Reply With Quote #1

Hello,

which one of the following "pseudo" codes is better/faster?

first: having local variables in OnGameFrame, but fast variable accesses ...
PHP Code:
public OnGameFrame() {
    new 
Float:PlayerVel[3];

    for( new 
client client <= MAXPLAYERS client++ ) {
        
GetEntPropVector(clientProp_Data"m_vecOrigin"PlayerVel);
        for ( new 
1000000 i++ ) PlayerVel[0] += 10;
    }

second: global variables instead of local ones, but a lot of array indexes ...
PHP Code:
new Float:PlayerVel[MAXPLAYERS][3];

public 
OnGameFrame() {
    for( new 
client client <= MAXPLAYERS client++ ) {
        
GetEntPropVector(clientProp_Data"m_vecOrigin"PlayerVel[client]);
        for ( new 
1000000 i++ ) PlayerVel[client][0] += 10;
    }

thx for help
__________________
Tauphi is offline
Send a message via ICQ to Tauphi Send a message via Skype™ to Tauphi
SAMURAI16
BANNED
Join Date: Sep 2006
Old 09-19-2008 , 12:56   Re: Optimization question: which code is better?
Reply With Quote #2

depends by your plugin. Use first method anyway
SAMURAI16 is offline
Send a message via MSN to SAMURAI16
Kigen
BANNED
Join Date: Feb 2008
Old 09-19-2008 , 13:07   Re: Optimization question: which code is better?
Reply With Quote #3

Code:
new Float:PlayerVel[3];

public OnGameFrame() {
    for( new client = 1 ; client <= MAXPLAYERS ; client++ ) {
        GetEntPropVector(client, Prop_Data, "m_vecOrigin", PlayerVel);
        for ( new i = 0 ; i < 1000000 ; i++ ) PlayerVel[0] += 10;
    }
}
Though your for 1000000 loop is going to kill any server that runs that code. But declaring it globally and then using it like it was declared locally is fine as long as no other function depends on it to be global. (SM code runs in a single threaded environment for the most part.
Kigen is offline
Tauphi
SourceMod Donor
Join Date: Sep 2004
Location: Germany
Old 09-19-2008 , 15:41   Re: Optimization question: which code is better?
Reply With Quote #4

i dont want to use the code ... i just wanted to know which algo is faster/better ;D
__________________
Tauphi is offline
Send a message via ICQ to Tauphi Send a message via Skype™ to Tauphi
Kigen
BANNED
Join Date: Feb 2008
Old 09-20-2008 , 00:40   Re: Optimization question: which code is better?
Reply With Quote #5

Well, the code posted in my last post is faster than both the ones you had in your original post.

Though if you handle it right you can use "decl" instead of "new" and that will also be faster. Though if you define something globally just use "new." "decl" should be used in commonly or heavily used functions.

As documented, "new" allocates the memory and 0's it out so its safe to be used immediately by a function. "decl" does allocates the memory but does not 0's it out so it is NOT safe to be used before a value is assigned to it.
Kigen is offline
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 20:13.


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