Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Do 'if' statements branch code in Lua?

                  
   
  1. #1
    GP2X Coder/Moderator
    Join Date
    Jan 2006
    Posts
    1,678
    Rep Power
    87

    Default Do 'if' statements branch code in Lua?

    Do 'if' statements branch code in Lua? (or scripting languages in general)

  2. #2
    DCEmu Coder dalejrrocks's Avatar
    Join Date
    Feb 2006
    Location
    Alabama
    Age
    34
    Posts
    240
    Rep Power
    71

    Default

    hm.. what exactly do you mean by branch? You can execute code selectively if that's what you mean. so i guess yeah.

  3. #3
    GP2X Coder/Moderator
    Join Date
    Jan 2006
    Posts
    1,678
    Rep Power
    87

    Default

    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?

  4. #4
    PS3 User Gizmo356's Avatar
    Join Date
    Feb 2006
    Age
    33
    Posts
    1,756
    Rep Power
    124

    Default

    un yeah i hope

  5. #5
    GP2X Coder/Moderator
    Join Date
    Jan 2006
    Posts
    1,678
    Rep Power
    87

    Default

    Quote Originally Posted by gizmo356
    un yeah i hope
    ???

  6. #6
    PS3 User Gizmo356's Avatar
    Join Date
    Feb 2006
    Age
    33
    Posts
    1,756
    Rep Power
    124

    Default

    i ment yes

  7. #7
    GP2X Coder/Moderator
    Join Date
    Jan 2006
    Posts
    1,678
    Rep Power
    87

    Default

    yes to which piece of information?

    (Side note: I have updated my second reply)

  8. #8
    PS3 User Gizmo356's Avatar
    Join Date
    Feb 2006
    Age
    33
    Posts
    1,756
    Rep Power
    124

    Default

    if if statements branch code in lua

  9. #9
    GP2X Coder/Moderator
    Join Date
    Jan 2006
    Posts
    1,678
    Rep Power
    87

    Default

    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.

  10. #10
    DCEmu Coder dalejrrocks's Avatar
    Join Date
    Feb 2006
    Location
    Alabama
    Age
    34
    Posts
    240
    Rep Power
    71

    Default

    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.

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •