Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: High CPU usage with very basic SFML program  (Read 20808 times)

0 Members and 2 Guests are viewing this topic.

Rob92

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
High CPU usage with very basic SFML program
« on: March 24, 2016, 03:13:22 am »
I have really high cpu usage (max for 1 core) for my sfml program.
At first I thought it was something in my framework but the profiler showed that 97% of all performance came from external code.
So I created a very basic SFML program and I have the same issue:



Yellow text is Fraps, confirming the program runs with 60 fps.
The only way I get the cpu to calm down abit is by manually letting the thread goto sleep, but even then its using way more than I would expect.

Any thoughts?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: High CPU usage with very basic SFML program
« Reply #1 on: March 24, 2016, 09:16:55 am »
Don't use vertical sync and framerate limit at the same time, as stated in the official tutorial.
Posting the code itself than a picture of it, would be appreciated next time. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Rob92

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
Re: High CPU usage with very basic SFML program
« Reply #2 on: March 24, 2016, 12:58:31 pm »
Thanks for your reply,

So, apparantly it has something to do with Vsync alone, when I set framerate limit to 60 but dont set vsync, it uses almost no CPU.
However, setting vsync to true and not setting framerate limit still keeps the CPU usage to 25%

Also maybe noteworthy, when I dont set frame limit or vsync, it runs around 8000 fps but uses LESS cpu than it would when I set vsync to true and running on 60 fps.
« Last Edit: March 24, 2016, 01:01:00 pm by Rob92 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: High CPU usage with very basic SFML program
« Reply #3 on: March 24, 2016, 02:49:49 pm »
Sounds like your GPU driver is doing a bad job at vsync. ;)
Is your GPU driver uptodate?

Do you run other "graphical" applications in the background?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Rob92

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
Re: High CPU usage with very basic SFML program
« Reply #4 on: March 24, 2016, 04:54:30 pm »
Hmm, I just updated my gfx driver and closed any possible graphical process in the background but it didnt make any difference.

I dont know if it's usefull info but I have a Nvidia GeForce GTX 970 in my desktop.
I will try running on my laptop later today, see if that makes a difference.

Rob92

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
Re: High CPU usage with very basic SFML program
« Reply #5 on: March 24, 2016, 07:41:12 pm »
OK, it seems like it runs fine on my laptop, only using 0,4% cpu at 60fps.
Don't really understand whats wrong with my pc then.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: High CPU usage with very basic SFML program
« Reply #6 on: March 25, 2016, 04:34:45 am »
Its very much possible that the driver authors were lazy and implemented vsync with a busy wait loop  :P
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: High CPU usage with very basic SFML program
« Reply #7 on: March 26, 2016, 02:29:53 pm »
It's old SFML bug. You should call setframelimit even if you don't using it (just pass zero). Otherwise you will get high cpu usage. And you should not use vblank sync and frame limit simultaneously.

So, try to use:
SetFrameLimit(0);
SetVerticalSyncEnabled(true);

It help me to solve this issue
« Last Edit: March 26, 2016, 02:42:42 pm by mkalex777 »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: High CPU usage with very basic SFML program
« Reply #8 on: March 26, 2016, 04:29:56 pm »
It's old SFML bug. You should call setframelimit even if you don't using it (just pass zero). Otherwise you will get high cpu usage. And you should not use vblank sync and frame limit simultaneously.

So, try to use:
SetFrameLimit(0);
SetVerticalSyncEnabled(true);

It help me to solve this issue

Will you please stop posting false information, there never was, never has been a bug that required you to call SetFramerateLimit if you didn't plan on using it.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: High CPU usage with very basic SFML program
« Reply #9 on: May 12, 2016, 02:11:25 am »
there never was, never has been a bug that required you to call SetFramerateLimit if you didn't plan on using it.

you're posting false information.
If you don't call SetFrameLimit(0) you will get high CPU load even with SetVerticalSyncEnabled(true).
At least SFML.NET has this issue.

This topic is related to SFML.NET which has this issue 100%, so please stop posting false information.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: High CPU usage with very basic SFML program
« Reply #10 on: May 17, 2016, 04:43:17 pm »
you're posting false information.
If you don't call SetFrameLimit(0) you will get high CPU load even with SetVerticalSyncEnabled(true).
At least SFML.NET has this issue.

This topic is related to SFML.Net which has this issue 100%, so please stop posting false information.

If you haven't noticed, I've been the one maintaining CSFML/SFML.NET and I am familiar with the entire code base. So unless you have facts to backup your claims stop making them.

Fact #1: SFML.Net does not reimplement SFML in C#, instead it is just a wrapper API on top of the CSFML API which is a wrapper on top of SFML. No functionality is changed.

Fact #2: CSFML/SFML.Net do not make any hidden calls to native SFML objects that you do not call yourself. This means that all SFML objects when initialized are in the same state as when you directly use SFML. So if you have to call SetFramerateLimit(...) in SFML.Net you would also have to do it directly in C++ to avoid this phantom 'bug' you describe.

var window = new RenderWindow(....);
window.SetVerticalSyncEnabled(true);
// No other calls

... is absolutely the same functionality wise as:

sf::RenderWindow window(....);
window.setVerticalSyncEnabled(true);
// No other calls

So until you are ready to prove how the functionality differs or do your own debugging as to why you think SFML.Net is responsible for something that is simply not the case, stop spreading your false information. I can talk all I want about how I landed on the moon during the Apollo missions (no I actually didn't), but until I actually show proof that I did that its just blatant lies. All you have done is make a claim without showing any proof whatsoever.
« Last Edit: May 17, 2016, 04:50:50 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: High CPU usage with very basic SFML program
« Reply #11 on: July 25, 2017, 04:04:36 pm »
So until you are ready to prove how the functionality differs or do your own debugging as to why you think SFML.Net is responsible for something that is simply not the case, stop spreading your false information.

I don't know why, but there is a difference.
I was used program with a lot of geometry drawing (lines, circles, rectangles) for testing.
I added call SetFramerateLimit(0) at program initialization and tried to run the program with this call and without (just commented it).

With the call SetFramerateLimit(0) I got 0% CPU load in the task manager.
When I commented this call, I got 25% CPU load (100% load for single CPU core).
I don't know the reason. May be there is OpenGL issue, may be there is SFML issue.
But the difference definitely exists.
In order to decrease CPU load, there is need for an explicit call to SetFramerateLimit(0).

 

anything