Page 1 of 1

Performance: should changing from if conditions to calling different functions be preferred?

Posted: Mon Jun 01, 2020 1:38 pm
by vityafx
Suppose I have a code in the `draw`:

Code: Select all

function Running:draw()
    if self.paused then
        self:draw_pause()
        self:draw_hud()
        return
    end

    self:draw_game()
end
In the keypress handler which then sets self:paused to true, there are two different ways one could do:

1. Add this if statement into the draw() call and check every render request if we are paused and decided what to draw based on that.
2. In the pause handler change the draw function of `self` to draw only pause stuff, and when the unpause, change it only to call `draw_game`.

I suppose we perform more useless operations checking every frame whether we are paused which we can simply avoid by directing the code what to draw exactly. This should increase performance. I couldn't find any Lua or LuaJIT documentation about this, unfortunately.

Re: Performance: should changing from if conditions to calling different functions be preferred?

Posted: Mon Jun 01, 2020 2:41 pm
by 4vZEROv
Premature optimization. Just do it like you think it make more sense.

Re: Performance: should changing from if conditions to calling different functions be preferred?

Posted: Mon Jun 01, 2020 2:51 pm
by pgimeno
I do believe that premature optimization on certain areas is the right thing to do, but you need some common sense for when to apply it. You don't need it for something that runs only once per frame.