High cpu usage [solved]

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
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

High cpu usage [solved]

Post by xNick1 »

Hi guys,
My game has a high CPU usage: ~25%.
I think it's a little too high for a pixel art game.
I don't know what could cause it, the game is pretty big already.

What are the operations which could take a high amount of CPU?
I make a lot of calculations but it's simple math
Last edited by xNick1 on Sun Sep 09, 2018 5:34 pm, edited 3 times in total.
Nelvin
Party member
Posts: 124
Joined: Mon Sep 12, 2016 7:52 am
Location: Germany

Re: High cpu usage

Post by Nelvin »

Check your FPS ... maybe you're running your game without vsync and so with a ridiculously high framerate resulting in your logic running, like 500 times a second.
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: High cpu usage

Post by xNick1 »

Nelvin wrote: Fri Sep 07, 2018 10:10 am Check your FPS ... maybe you're running your game without vsync and so with a ridiculously high framerate resulting in your logic running, like 500 times a second.
Hmm I'm using vsync, I have 60 stable fps.
Does the audio impact much on the cpu?

Edit: It seems I'm messing something up with the audio.
In normal circumstances the game doesn't use over 4% of cpu, until I mess up with the audio.

I want to play some music when I'm near the castle.
The background music has to fade out, and the castle music has to fade in

castle:

Code: Select all

CastleSummer = Object:extend()
local imgPath = "assets/images/summer/"
local img = lg.newImage(imgPath .. "castle/castle.png")
img:setFilter('nearest', 'nearest')
local width = wWidth / 2 --wWidth / 1.5
local height = wHeight / 1.6 --wHeight / 1.3 
local sx = width / img:getWidth()
local sy = height / img:getHeight()
local y = wHeight - height
local music = la.newSource("assets/sounds/summer/castle.ogg") 

function CastleSummer:new(x)
    self.x = x
    self.y = y + wHeight / 70
    self.width = width
    self.height = height
    
    self.music = music
    self.music:setLooping(true)
    self.music:play()
    self.volume = 0
end

function CastleSummer:update(dt)
    self.music:setVolume(self.volume)
end

function CastleSummer:draw()
    lg.draw(img, self.x, self.y, 0, sx, sy)
end

background music:

Code: Select all

BackgroundMusicSummer = Object:extend()

function BackgroundMusicSummer:new()
    self.music = la.newSource("assets/sounds/summer/summer.ogg")
    self.music:setLooping(false)
    self.volume = 1
end

function BackgroundMusicSummer:update(dt)
    
    self.music:setVolume(self.volume)
end

function BackgroundMusicSummer:draw()
end

function BackgroundMusicSummer:stop()
    self.music:stop()
end

function BackgroundMusicSummer:play()
    self.music:play()
end
map:

Code: Select all

function spawnCastleSummer(dt)
    timePassedCastleSpawn = timePassedCastleSpawn + 1 * dt
    if timePassedCastleSpawn > castleSpawnTime then
        timePassedCastleSpawn = 0
        castleSpawnTime = love.math.random(30, 60)
        local r = playerSummer.x + wWidth
        local castleSummer = CastleSummer(r)
        table.insert(castles, castleSummer)
    end
    
    for i, c in ipairs(castles) do
        c:update(dt)
        
        if c.x < playerSummer.x - wWidth * 4 then
            table.remove(castles, i)
        end
    end
end

function collisionsCastleSummer(dt)
    for _, c in ipairs(castles) do
        if playerSummer.x >= c.x and playerSummer.x <= c.x + c.width then
            backgroundMusicSummer.volume = backgroundMusicSummer.volume - 0.2 * dt
            if backgroundMusicSummer.volume <= 0 then
                backgroundMusicSummer.volume = 0.1
            end
            
            c.volume = c.volume + 0.4 * dt
            if c.volume >= 1 then
                c.volume = 1
            end

        else
            c.volume = 0
            backgroundMusicSummer.volume = backgroundMusicSummer.volume + 0.2 * dt
            if backgroundMusicSummer.volume >= 1 then
                backgroundMusicSummer.volume = 1
            end
        end
    end
end
Nelvin
Party member
Posts: 124
Joined: Mon Sep 12, 2016 7:52 am
Location: Germany

Re: High cpu usage [updated, problems with the Audio]

Post by Nelvin »

That's strange - have you verified that removing these two background audio sources reduces the CPU load?

I just added to background songs to my game and changed their volume each frame with no measurable difference in cpu usage.
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: High cpu usage [updated, problems with the Audio]

Post by xNick1 »

Wops, I feel dumb now.
The audio thing was one of the latest things I added, but the issue is still there after removing the music all together.
The cpu usage increases regularly as I play the game.
I spawn many objects while I play, but it's just simple sprites.
I'm definitely doing something wrong, but the project is big already and I have to figure out what I'm doing wrong.

Thanks for the help

The sprites are supposed to end up in the ram right?
What could even even use that much cpu?
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: High cpu usage [updated, problems with the Audio]

Post by grump »

xNick1 wrote: Fri Sep 07, 2018 3:24 pm I spawn many objects while I play, but it's just simple sprites.
How many is 'many'? Are you creating lots of (temporary) tables? Garbage collection is known to be a common bottleneck if the code makes poor use of tables.
I'm definitely doing something wrong, but the project is big already and I have to figure out what I'm doing wrong.
If you can't figure it out with the source code right before your eyes, you can't expect others to figure it out without looking at your code.
The sprites are supposed to end up in the ram right?
Textures are stored on the GPU. Anything else is stored in RAM.
What could even even use that much cpu?
Literally anything. Impossible to say without looking at the code.

Search the forums for "profiler". IIRC, ivan made a general purpose profiler that you could use to find bottlenecks in your code.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: High cpu usage [updated, problems with the Audio]

Post by pgimeno »

grump wrote: Fri Sep 07, 2018 3:46 pm If you can't figure it out with the source code right before your eyes, you can't expect others to figure it out without looking at your code.
Spot-on. May I borrow that sentence? It can be useful in more than one occasion. :)
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: High cpu usage

Post by xNick1 »

I finally found the real problem.
I was running a twin with flux in an update function.
I didn't really need the other optimizations
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 53 guests