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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
vityafx
Prole
Posts: 4
Joined: Sat Nov 11, 2017 5:59 pm

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

Post 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.
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

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

Post by 4vZEROv »

Premature optimization. Just do it like you think it make more sense.
User avatar
pgimeno
Party member
Posts: 3593
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest