PDA

View Full Version : Do 'if' statements branch code in Lua?



yaustar
July 22nd, 2006, 00:27
Do 'if' statements branch code in Lua? (or scripting languages in general)

dalejrrocks
July 24th, 2006, 16:25
hm.. what exactly do you mean by branch? You can execute code selectively if that's what you mean. so i guess yeah.

yaustar
July 24th, 2006, 18:43
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?

Gizmo356
July 24th, 2006, 18:54
un yeah i hope

yaustar
July 25th, 2006, 05:37
un yeah i hope
???

Gizmo356
July 25th, 2006, 05:41
i ment yes

yaustar
July 25th, 2006, 05:43
yes to which piece of information?

(Side note: I have updated my second reply)

Gizmo356
July 25th, 2006, 05:51
if if statements branch code in lua

yaustar
July 25th, 2006, 06:03
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

-- 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

The screen shot shows the number of ms per function call.
http://i7.tinypic.com/20v16h1.png

dalejrrocks
July 26th, 2006, 01:27
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.

yaustar
July 26th, 2006, 01:32
Here is the kicker though, originally I had "State = 0" so the if statement was false, the IfTest was faster the NoOfTest ??? I have to do a similar test at some point in C/C++ and use the -03 flag and see how big (if any) a difference it makes.

dalejrrocks
July 26th, 2006, 20:23
well, that makes sense. i guess an if loop can be faster than the for one sometimes. but like you said there's not really a significant difference in speed.

yaustar
July 26th, 2006, 22:51
I suppose it is due to scripts being interpreted rather then pre-compiled and there is no (?) optimisation stage when the script is ran 'on the fly' so the difference is negible if any.