CPU Usage Spiking

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
cephalopod11
Prole
Posts: 3
Joined: Sat Aug 08, 2020 1:13 am

CPU Usage Spiking

Post 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!
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: CPU Usage Spiking

Post 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
cephalopod11
Prole
Posts: 3
Joined: Sat Aug 08, 2020 1:13 am

Re: CPU Usage Spiking

Post 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!
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: CPU Usage Spiking

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: CPU Usage Spiking

Post 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.
cephalopod11
Prole
Posts: 3
Joined: Sat Aug 08, 2020 1:13 am

Re: CPU Usage Spiking

Post 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 :)
Post Reply

Who is online

Users browsing this forum: No registered users and 139 guests