Page 1 of 1

CPU Usage Spiking

Posted: Sat Aug 08, 2020 1:33 am
by cephalopod11
Hello all. New to lua and love (I use Python in my day job), but I'm a big fan already. I'm hoping for some help in figuring out why my game is eating up so much CPU.

It's a relatively static game where the player clicks letter buttons to try to create words, so very little is actually happening in the update loop (just a counter to determine how long certain messages stay on the screen).

The full code can be found here: https://git.lambda.church/max/Spellspiel Please let me know if there's a better way to share it.

I just added the blockFPS function in main.lua, which is something I'd seen in another forum post about CPU use. It does help, but I'm trying to understand what that's needed in my game though. Why is it using so much CPU in the first place?

One theory is that it has something to do with my many font size changes, which are mostly getting called inside various draw() calls. If that is one of the causes, is there a better way to do this?

Thank you all in advance for any advice you can give!

Re: CPU Usage Spiking

Posted: Sat Aug 08, 2020 6:02 pm
by grump
cephalopod11 wrote: Sat Aug 08, 2020 1:33 am One theory is that it has something to do with my many font size changes, which are mostly getting called inside various draw() calls. If that is one of the causes, is there a better way to do this?
Create all font sizes beforehand, don't do it every frame.

Code: Select all

local font1 = love.graphics.newFont(32)
local font2 = love.graphics.newFont(48)

function love.draw()
  love.graphics.setFont(font1)
  love.graphics.print(...)
  love.graphics.setFont(font2)
  love.graphics.print(...)
end

Re: CPU Usage Spiking

Posted: Sat Aug 08, 2020 11:15 pm
by cephalopod11
Thanks, grump! That makes sense. I'll make those changes and hopefully some of the CPU usage will come down. Appreciate the info!

Re: CPU Usage Spiking

Posted: Sun Aug 09, 2020 8:44 am
by zorg
Also, you have a pairs iterator to count a table in your utils lua file... that also gets called twice on different tables in one of your draw callbacks, iirc in game.lua; do try to see if it would be possible to use numeric arrays instead, since they can use the # function to tell (consequent) elements... and it would be a lot faster considering the size of your dictionary table.

Re: CPU Usage Spiking

Posted: Sun Aug 09, 2020 10:44 am
by pgimeno
It's fine; that table contains only the words corresponding to the current letter combo, which can be in the order of 40 or so.

Counting them every frame should not be necessary anyway. On one hand, createNewGame can count the ones in the dict and store the count in a variable; on the other hand, a running counter can increment every time the user enters a valid word.

That looks like an unnecessary optimization in this case, though.

This game reminds me a lot of sphyrth's Word Whacker, by the way, except that you can reuse letters.

Re: CPU Usage Spiking

Posted: Mon Aug 10, 2020 2:33 am
by cephalopod11
Ah, cool! Thanks for sharing that other game. Mine is based on the NY Times' "Spelling Bee," which only offers one game each day. I basically just wanted to play more than once some days :)