Do 'if' statements branch code in Lua? (or scripting languages in general)
Printable View
Do 'if' statements branch code in Lua? (or scripting languages in general)
hm.. what exactly do you mean by branch? You can execute code selectively if that's what you mean. so i guess yeah.
Not 100% sure how to explain. When you compile C code and place an if statement in an intensive loop, it runs a lot slower then if it didn't have the if. This is because the compiler cannot optimise the code very well since it cannot predict the 'code path' (if that makes any sense) for runtime. This leads to performence loss.
eg:
(C++ code)
for ( int i = 0; i < 5000; ++i)
{
if (object.state == true)
{
object.yposition += 10;
}
}
will run slower (?) then
for ( int i = 0; i < 5000; ++i)
{
object.yposition += (10 * object.state);
}
If I did something similar in Lua would I get any speed gain using the second codebit rather then the first?
un yeah i hope
???Quote:
Originally Posted by gizmo356
i ment yes
yes to which piece of information?
(Side note: I have updated my second reply)
if if statements branch code in lua
You misunderstood the question (or I worded it badly). The question is about the performence rather then the functionality of an if statement in Lua.
In short, would there be any performence difference between:
for i=0, 5000 do
if object.state == true then
object.yposition = object.yposition + 10
end
end
or
for i=0, 5000 do
object.yposition = object.yposition + (10 * object.state)
end
And if so, how much (and why).
Edit: Surprisingly, there doesn't seem to be that big a difference
The screen shot shows the number of ms per function call.Quote:
-- Disclaimer: If you do use this code in any published lua script/apllication/game, you MUST agree to credit the author
--2006-07-24:
-- yaustar: Speed test
-- Preload some colours
GREEN = Color.new(0, 255, 0);
RED = Color.new(255, 0, 0);
BLUE = Color.new(0, 0, 255);
WHITE = Color.new(255, 255, 255);
BLACK = Color.new(0, 0, 0);
PINK = Color.new(255, 0, 255);
-- true for Key Up, false for Key Down for readability
Bool_KeyUp = true;
Bool_KeyDown = false;
-- List of bools to store the last state of the Key
Bool_PadDLeftLastState = Bool_KeyUp;
Bool_PadDRightLastState = Bool_KeyUp;
Bool_PadDUpLastState = Bool_KeyUp;
Bool_PadDDownLastState = Bool_KeyUp;
Bool_PadDLbutLastState = Bool_KeyUp;
Bool_PadDRbutLastState = Bool_KeyUp;
Bool_PadDSqaureLastState = Bool_KeyUp;
Bool_PadDTriangleLastState = Bool_KeyUp;
Bool_PadDCrossLastState = Bool_KeyUp;
Bool_PadDCircleastState = Bool_KeyUp;
Bool_PadDSelectLastState = Bool_KeyUp;
Bool_PadDStartLastState = Bool_KeyUp;
-- Blank the screen
screen:clear(WHITE);
Int_CurrentLine = 0;
-- Go to the next line
function BlankLine()
Int_CurrentLine = Int_CurrentLine + 1;
end
-- String varaible to hold text
Str_LineOfText = "";
-- Create an object
Object =
{
YPosition = 0,
State = 1
};
-- Create a timer
Clock = Timer.new();
-- Get the number of milliseconds since last call
function GetDeltatime()
local Int_Dtime = Clock:time();
Clock:reset();
Clock:start();
return Int_Dtime;
end
-- If Test function
function IfTest()
for i=0,500000 do
if Object.State == 1 then
Object.YPosition = Object.YPosition + 10;
end
end
end
function NoIfTest()
for i=0,500000 do
Object.YPosition = Object.YPosition + (10 * Object.State);
end
end
-- Print it out
screen:print(1, Int_CurrentLine * 8, "> Using the If Statement:", BLUE );
BlankLine();
for i=0,5 do
GetDeltatime();
IfTest();
local DTime = GetDeltatime();
screen:print(1, Int_CurrentLine * 8, DTime, BLACK );
BlankLine();
end
-- Print it out
screen:print(1, Int_CurrentLine * 8, "> Using no if Statement:", BLUE );
BlankLine();
for i=0,5 do
GetDeltatime();
NoIfTest();
local DTime = GetDeltatime();
screen:print(1, Int_CurrentLine * 8, DTime, BLACK );
BlankLine();
end
-- Wait for Vblank
screen.waitVblankStart(0);
-- Flip the buffers twice because of double buffering
screen.flip();
screen.flip();
-- If the player has pressed start, exit the loop and end the 'game'
while true do
-- Get the pad state
PspPadState = Controls.read();
if PspPadState:start() then
break;
end
end
http://i7.tinypic.com/20v16h1.png
wow, there's actually some difference. the results represent what i was thinking. i was thinking that the if one would be slower because of the constant checking of that statement, while the for one would just go through it quickly. nice test.