how to reduce CPU usage ?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
wahaha
Prole
Posts: 2
Joined: Mon Oct 09, 2017 4:34 am

how to reduce CPU usage ?

Post 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 ?
User avatar
TC1061
Prole
Posts: 32
Joined: Sat Oct 14, 2017 3:50 pm

Re: how to reduce CPU usage ?

Post 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
hamberge
Prole
Posts: 25
Joined: Wed Aug 16, 2017 2:55 pm

Re: how to reduce CPU usage ?

Post by hamberge »

How long is your code?
Fuzzlix
Citizen
Posts: 60
Joined: Thu Oct 13, 2016 5:36 pm

Re: how to reduce CPU usage ?

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

Re: how to reduce CPU usage ?

Post 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.
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
TC1061
Prole
Posts: 32
Joined: Sat Oct 14, 2017 3:50 pm

Re: how to reduce CPU usage ?

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

Re: how to reduce CPU usage ?

Post by zorg »

Because calling love.timer.sleep (more than once each frame) should best be avoided, since it blocks execution.
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.
wahaha
Prole
Posts: 2
Joined: Mon Oct 09, 2017 4:34 am

Re: how to reduce CPU usage ?

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

Re: how to reduce CPU usage ?

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

Who is online

Users browsing this forum: No registered users and 10 guests