Page 1 of 1

High CPU consumption

Posted: Sun Oct 20, 2019 5:37 pm
by bphil
Dear forum,

I am playing around with love and cobbled together the attached code in the process.

Now I noticed that the fan on my notebook (Carbon X1, 4xIntel(R) Core(TM) i7-7600U CPU @ 2.80GHz, 16GB ram) is starting up when I launch the game and I was wondering why that would be, given that the code is really not doing much. I looked around in the forum and

- excluded the vsync issue (or, at least my FPS is constant at 60, which I took to exclude the vsync issue);
- activated itraykov's profiler, but the output is not telling me very much, to be honest (the only thing I noted is that in comparison with the example report in the profiler's documentation, I get a lot of calls (100) for some of my functions).

So I wonder whether anyone of you could advise on what is eating up my CPU or on how to proceed looking into this.

Many thanks!
Philipp

Re: High CPU consumption

Posted: Sun Oct 20, 2019 8:45 pm
by pgimeno
It's the calls to love.graphics.print() which are killing it. Maybe try to use a pre-rendered Text object.

I can't advise further because I have no idea what this is about; all I see is this (cropped because it's too big):

Re: High CPU consumption

Posted: Mon Oct 21, 2019 6:41 am
by bphil
Thank you very much for your reply.

This is good news, as the numbers were just there for debug reasons (there is really not much more to this at the moment than what you saw; I am simply learning how to use the gamera and push libraries and how to correlate mouse position with places in the game).

May I ask how you determined that it's the printing of the numbers that caused the CPU consumption? Is that just a general fact about the nature of rendering text that it is demanding for the CPU or did you do an analysis of some sort, which I could repeat should similar issues arise in the future?

Thanks again and best wishes,
Philipp

Re: High CPU consumption

Posted: Mon Oct 21, 2019 10:07 am
by pgimeno
bphil wrote: Mon Oct 21, 2019 6:41 am(there is really not much more to this at the moment than what you saw;
What I saw is what the image shows literally, which I cropped but I did NOT scale down. The text was not readable, and the lines were barely visible. I'm letting you know in case you want to reconsider the target screen size for people with smaller screens (mine is (1280x1024).

bphil wrote: Mon Oct 21, 2019 6:41 amMay I ask how you determined that it's the printing of the numbers that caused the CPU consumption?
I disabled parts of the program and checked CPU %, until I got there. First I disabled the call in love.update, but that caused a crash; apparently it's defining something that love.draw needs. Then I disabled the EditorDraw call in love.draw and that got totally rid of the CPU consumption. I looked in the target function and disabled it in parts; that led me to drawGrid. Lastly, the CPU consumption dropped when I disabled love.graphics.print in there.

Re: High CPU consumption

Posted: Mon Oct 21, 2019 12:02 pm
by Luke100000
I assume that the CPU usage is only from preparing the text, not rendering. So, as pgimeno said, a text object should fix that. (not tested yet)

If this is not possible, then at least only draw text visible on screen. I tested this and achieved 4% CPU usage instead of 25%.

Re: High CPU consumption

Posted: Wed Oct 23, 2019 7:53 am
by bphil
Thanks for pointing out the debug procedure, pgimeno.

Thank you also for your suggestion, Luke100000. As I said in my reply to pgimeno, the text is only there for debug reasons. Nevertheless, I think it's a good exercise to restrict drawing to what is visible. Given that you were able to quickly test the effect of drawing only what is visible on screen, I assume it is not very hard to integrate. How did you do it? Did you just put the love.graphics.print-call that draws the numbers into an if-clause that compares the camera coordinates with the coordinates at which the numbers would be drawn?

Re: High CPU consumption

Posted: Thu Oct 24, 2019 5:34 am
by raidho36
That is one way to do it, but since you're iterating over coordinates anyway you can limit the coordinates in the loop parameters.

Re: High CPU consumption

Posted: Thu Oct 24, 2019 10:48 am
by Luke100000
As raidho34 said, I just limited the x and y loop.

Usually drawing outside the screen (images) won't really affect the performance, this is already handled by the GPU (or something before, I'm not sure). But text requires some pre processing.

So don't check every image manually. Limit mass draws like loops or if you have complex objects with several images check only the object. But in other cases I do not recommend additional calculations. (you would also have to consider transformations, ...)