Raised This Month: $32 Target: $400
 8% 

Optimizing Plugins - discussion


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Casio
Member
Join Date: Jan 2005
Old 08-28-2006 , 15:46   Optimizing Plugins - discussion
Reply With Quote #1

If there is already a thread about this I apologise now and expect this thread will no doubt be deleted accordingly.

http://wiki.amxmodx.org/index.php/Optimizing_Plugins

After being told about this optimizing plugins site (thankyou Suicid3 ) I was hoping we could get some of the "pro" Pawn programmers to take time out of their busy schedules and answer some questions from the "inexperienced" programmers amongst us who sometimes wonder if their plugins are the cause of lag on their native servers.

I'll go first; Re-using variables.

The above site does not mention re-using variables so I was wondering, which of the two code fragments below is most efficient or better?

#1
Code:
new cc = ns_get_build ( "team_command" , 1 , 1 ) //Get CC index
new Float: fl_Tmp

fl_Tmp = entity_get_float ( cc, EV_FL_health ) //Get CC health
new ccHP = floatround ( fl_Tmp )

fl_Tmp = entity_get_float ( cc, EV_FL_max_health ) //Get CC max health
new ccMaxHP = floatround ( fl_Tmp )

fl_Tmp = (ccHP / ccMaxHP) * 100  //get CC health percentage
new ccPercent = floatround ( fl_Tmp )
#2
Code:
new cc = ns_get_build ( "team_command" , 1 , 1 ) //Get CC index

new Float:fl_ccHp = entity_get_float ( cc, EV_FL_health ) //Get CC health
new ccHp = floatround ( fl_ccHp )

new Float:fl_ccMaxHp = entity_get_float ( cc, EV_FL_max_health ) //Get CC max hp
new ccMaxHp = floatround ( fl_ccMaxHp )

new Float:fl_Percent = ( ccMaxHp / ccMaxHP) * 100  //get CC health percentage
new ccPercent = floatround ( fl_Percent )
Casio is offline
Casio
Member
Join Date: Jan 2005
Old 08-28-2006 , 15:58   Re: Optimizing Plugins - discussion
Reply With Quote #2

Question #2:

Someone once told me this code fragment is better:

Code:
new tmp = is_user_alive ( id )
if (tmp == 1) {
	//code
}
than this code fragment:
Code:
if (is_user_alive ( id ) == 1) {
	//code
}
Is this true?
Casio is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 08-28-2006 , 16:52   Re: Optimizing Plugins - discussion
Reply With Quote #3

Quote:
Originally Posted by Casio View Post
If there is already a thread about this I apologise now and expect this thread will no doubt be deleted accordingly.

http://wiki.amxmodx.org/index.php/Optimizing_Plugins

After being told about this optimizing plugins site (thankyou Suicid3 ) I was hoping we could get some of the "pro" Pawn programmers to take time out of their busy schedules and answer some questions from the "inexperienced" programmers amongst us who sometimes wonder if their plugins are the cause of lag on their native servers.

I'll go first; Re-using variables.

The above site does not mention re-using variables so I was wondering, which of the two code fragments below is most efficient or better?

#1
Code:
new cc = ns_get_build ( "team_command" , 1 , 1 ) //Get CC index
new Float: fl_Tmp

fl_Tmp = entity_get_float ( cc, EV_FL_health ) //Get CC health
new ccHP = floatround ( fl_Tmp )

fl_Tmp = entity_get_float ( cc, EV_FL_max_health ) //Get CC max health
new ccMaxHP = floatround ( fl_Tmp )

fl_Tmp = (ccHP / ccMaxHP) * 100  //get CC health percentage
new ccPercent = floatround ( fl_Tmp )
#2
Code:
new cc = ns_get_build ( "team_command" , 1 , 1 ) //Get CC index

new Float:fl_ccHp = entity_get_float ( cc, EV_FL_health ) //Get CC health
new ccHp = floatround ( fl_ccHp )

new Float:fl_ccMaxHp = entity_get_float ( cc, EV_FL_max_health ) //Get CC max hp
new ccMaxHp = floatround ( fl_ccMaxHp )

new Float:fl_Percent = ( ccMaxHp / ccMaxHP) * 100  //get CC health percentage
new ccPercent = floatround ( fl_Percent )
The first one is better.

Quote:
Originally Posted by Casio View Post
Question #2:

Someone once told me this code fragment is better:

Code:
new tmp = is_user_alive ( id )
if (tmp == 1) {
    //code
}
than this code fragment:
Code:
if (is_user_alive ( id ) == 1) {
    //code
}
Is this true?
Wrong. The second one is faster. If you use tmp twice though, or need to check is_user_alive twice, then the first one is better.
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
MaximusBrood
Veteran Member
Join Date: Sep 2005
Location: The Netherlands
Old 08-28-2006 , 16:52   Re: Optimizing Plugins - discussion
Reply With Quote #4

Short Answers:

#1: First one
#2: No

---

Long Answers:

#1: First one, but it won't differ very much.
#2: It is actually faster, but it is slower if you want to know if the user was dead or alive later in your code.

---

EDIT: Argh, too late
__________________
Released six formerly private plugins. Not active here since ages.
MaximusBrood is offline
Casio
Member
Join Date: Jan 2005
Old 08-28-2006 , 21:24   Re: Optimizing Plugins - discussion
Reply With Quote #5

Quote:
Originally Posted by MaximusBrood View Post
EDIT: Argh, too late
The long answers got you lol.

Cheers for that guys
Casio is offline
Reply


Thread Tools
Display Modes

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:41.


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