 |
 |
 |
|
 |
 |
 |
|
 |
 |
Script optimization question |
|
1 2 3 4 | Next 10 events »| All Messages |
|
|
01:23:28 29 January 2013 |
|
Daemonion
All About Audio (Resident)
On forum: 09/27/2011
Messages: 567
|
What different in speed are we talking about here? A tenth of a second? |
10:35:35 29 January 2013 |
|
Decane
Senior Resident
On forum: 04/04/2007
 Message edited by: Decane 01/29/2013 10:56:06
Messages: 1705
|
I should not have written these in haste; I forgot to mention that in the case of B3 and B4, "friend" and "enemy" are the only possible values for the variable db.actor. So there is no "neutral" or anything else. However, if there were, then would B3 be faster? I suspect so because the "else" in B4 is doing the check, 'if not db.actor == "friend"', which seems to be equivalent to 'if db.actor == "enemy" or db.actor == "neutral"' with two other possibilities (???), so that the code in B4 is actually interpreted like this:
function r()
if db.actor == "friend" then
return
elseif db.actor == "neutral" or db.actor == "enemy" then
if db.actor == "enemy" then
return
end
end
end And of course, in this case, B4 would be slower since it is doing a redundant check. Is this correct?
--------------
Alundaio, thanks for those insights. GSC's code is filled to the brim with ipairs() ...
--------------
I have a few more questions. Which of these is faster, if either:
(A1)
function is_bandit_happy()
local bandit_mood = sim_faction.get_mood("bandit")
return (bandit_mood ~= "depressed")
and (bandit_mood ~= "a bit blue")
and (bandit_mood ~= "content")
end (A2)
function is_bandit_happy()
local bandit_mood = sim_faction.get_mood("bandit")
return not (bandit_mood == "depressed")
and not (bandit_mood == "a bit blue")
and not (bandit_mood == "content")
end Assume that our 'bandit mood table' does not contain a value for "happy". If I had to guess, I would assume that ("" ~= "") is shorthand for (not "" == ""), but in this case, lua would need to interpret the shorthand, whereas it would not need to do that with the reference notation. So in theory, (not "" == "") would be faster?
Also, can I do this in lua:
(A3)
function is_bandit_happy()
local bandit_mood = sim_faction.get_mood("bandit")
return bandit_mood ~= ("depressed" or "a bit blue" or "content")
end If so, will this make the code any faster?
--------------
Finally, a question about localizing variables (again): is it faster to push local variables inside a function as much as possible (i.e. to make their scope as narrow as possible) or is it better to define them so that their scope is as wide as possible (within the function)? Example:
(B1)
function bandit_is_happy()
local bandit_mood = sim_faction.get_mood("bandit")
if bandit_mood == "happy" then
local bandit_is_happy
-- define and do something with variable bandit_is_happy
end
end
vs.
(B2)
function bandit_is_happy()
local bandit_mood = sim_faction.get_mood("bandit")
local bandit_is_happy
if bandit_mood == "happy" then
-- define and do something with variable bandit_is_happy
end
end
Thanks for all the replies, guys. This has been very fruitful so far.
EDIT: Removed smilies.
• Sky Reclamation Project v1.1.2: http://www.moddb.com/mods/srp/downloads/srp-v112
• Sky Reclamation Project v1.1.3 WIP: https://github.com/Decane/SRP
|
|
1 2 3 4 | Next 10 events »| All Messages |
|
|
|
» » |
|
All short dates are in Month-Day-Year format. |
 |
|
 |
|
|
|
 |
 |
 |