Page 1 of 1

how to reduce CPU usage ?

Posted: Wed Oct 18, 2017 3:26 am
by wahaha
Hi guys, I recently notice that the CPU used by the love engine is round 13% on my computer. compared to other games like WOW is 5%.
So is there some way to reduce it? I also checked other engines like mono game, unity, it's all nearly the same round 13%. Is this just a normal case in today's engine ?

Re: how to reduce CPU usage ?

Posted: Wed Oct 18, 2017 10:48 am
by TC1061
You may decrease CPU usage by decreasing FPS. It's really easy!

Code: Select all

function blockFPS(max,dt) -- Call this function in love.update
	if dt < 1/max then
		love.timer.sleep(1/max-dt)
	end
end
function love.update(dt)
	blockFPS(24 --[[Maximum FPS]],dt --[[Deltatime]])
	--Something
end

Re: how to reduce CPU usage ?

Posted: Wed Oct 18, 2017 11:15 am
by hamberge
How long is your code?

Re: how to reduce CPU usage ?

Posted: Wed Oct 18, 2017 11:58 am
by Fuzzlix
wahaha wrote: Wed Oct 18, 2017 3:26 am Hi guys, I recently notice that the CPU used by the love engine is round 13% on my computer. compared to other games like WOW is 5%.
So is there some way to reduce it? I also checked other engines like mono game, unity, it's all nearly the same round 13%. Is this just a normal case in today's engine ?
You can activate VSYNC in your games conf.lua.
Otherwise loves main loop runs without being delayed.

Re: how to reduce CPU usage ?

Posted: Wed Oct 18, 2017 12:55 pm
by zorg
By any chance, do you have an 8-core CPU? 100 divided by 8 is 12.5, pretty close for your löve app to gobble up one core completely; enabling vsync is one solution, but even then, drivers may cause busy-waiting if not implemented differently, which would get you back to square one.

Also please don't do what TC1061 suggests; the default love.run already calls love.timer.sleep with 0.001 as parameter, meaning the top speed the game loop spins is around 1000 (FPS); if the vsync method doesn't work, you could redefine love.run to wait more, 1/60 if you want 60 FPS, etc., or use an accumulator variable that you add dt to, and only run love.update and love.draw if that goes over 1/60 or similar.

Re: how to reduce CPU usage ?

Posted: Wed Oct 18, 2017 1:08 pm
by TC1061
zorg wrote: Wed Oct 18, 2017 12:55 pm By any chance, do you have an 8-core CPU? 100 divided by 8 is 12.5, pretty close for your löve app to gobble up one core completely; enabling vsync is one solution, but even then, drivers may cause busy-waiting if not implemented differently, which would get you back to square one.

Also please don't do what TC1061 suggests; the default love.run already calls love.timer.sleep with 0.001 as parameter, meaning the top speed the game loop spins is around 1000 (FPS); if the vsync method doesn't work, you could redefine love.run to wait more, 1/60 if you want 60 FPS, etc., or use an accumulator variable that you add dt to, and only run love.update and love.draw if that goes over 1/60 or similar.
Why don't do? My solution isn't bad. It really reduces CPU usage. Maybe 24 FPS is too low :) ... But your solution is also good.

Re: how to reduce CPU usage ?

Posted: Wed Oct 18, 2017 2:06 pm
by zorg
Because calling love.timer.sleep (more than once each frame) should best be avoided, since it blocks execution.

Re: how to reduce CPU usage ?

Posted: Wed Oct 18, 2017 5:22 pm
by wahaha
zorg wrote: Wed Oct 18, 2017 12:55 pm By any chance, do you have an 8-core CPU? 100 divided by 8 is 12.5, pretty close for your löve app to gobble up one core completely; enabling vsync is one solution, but even then, drivers may cause busy-waiting if not implemented differently, which would get you back to square one.
thanks for the help. your right, I just added this " t.window.vsync = false " , now my cpu usage <= 1.
but this make some animation run fast, i need check delta time for more details, just a quick reply. thanks all

Re: how to reduce CPU usage ?

Posted: Wed Oct 18, 2017 7:27 pm
by zorg
wahaha wrote: Wed Oct 18, 2017 5:22 pm
zorg wrote: Wed Oct 18, 2017 12:55 pm By any chance, do you have an 8-core CPU? 100 divided by 8 is 12.5, pretty close for your löve app to gobble up one core completely; enabling vsync is one solution, but even then, drivers may cause busy-waiting if not implemented differently, which would get you back to square one.
thanks for the help. your right, I just added this " t.window.vsync = false " , now my cpu usage <= 1.
but this make some animation run fast, i need check delta time for more details, just a quick reply. thanks all
Yeah, you should either use dt in your code to ensure that everything runs equally fast on different computer configs, or implement a fixed timestep; accumulating dt in a variable until it's more than the delay you want (usually 1/60), then do an update once, and decrease the value by that amount.