SFML community forums

Help => General => Topic started by: Makuto on March 03, 2011, 03:39:32 am

Title: 100% CPU on simple program?
Post by: Makuto on March 03, 2011, 03:39:32 am
Why is my simple program taking up 100% of my CPU?  My code is from the documentation files (the first example).
Title: 100% CPU on simple program?
Post by: Laurent on March 03, 2011, 07:56:53 am
Because you tell it to take 100% CPU. Well, in fact you don't tell it to not take 100% CPU ;)

Simply use SetFramerateLimit, UseVerticalSync or Sleep to slow down your program and let the CPU do something else.
Title: 100% CPU on simple program?
Post by: Makuto on March 03, 2011, 03:40:38 pm
What would you suggest I limit the frame rate to?
Title: 100% CPU on simple program?
Post by: Laurent on March 03, 2011, 04:13:57 pm
Use vertical synchronization, it will give better visual results because the framerate will be synchronized with the monitor's refresh rate.
Title: 100% CPU on simple program?
Post by: Makuto on March 04, 2011, 12:45:43 am
Is that in the documentation?  I'm assuming it is.  Thanks for the help.
Title: 100% CPU on simple program?
Post by: Laurent on March 04, 2011, 07:49:52 am
It is, all functions are documented. I don't know if it's in the tutorials.
Title: 100% CPU on simple program?
Post by: Makuto on March 04, 2011, 02:28:22 pm
I used the UseVerticalSync and the program looks really good now, but it still takes up 100%.  Should I also limit the framerate?
Title: 100% CPU on simple program?
Post by: Laurent on March 04, 2011, 02:32:23 pm
Really? It shouldn't. Try SetFramerateLimit (without UseVerticalSync), and if it still doesn't work then you probably have a problem.
Title: 100% CPU on simple program?
Post by: Makuto on March 07, 2011, 02:28:14 pm
It's working now.  Thanks for all of the help!
Title: 100% CPU on simple program?
Post by: strongdrink on March 24, 2011, 04:06:04 pm
I have the same problem but it isn't working for me...
I'm using Ubuntu 10.10.

  App.UseVerticalSync (true);

or

  App.UseVerticalSync (false);
  App.SetFramerateLimit (0x20);

both limit the rate, but the CPU satys at 100%
Title: 100% CPU on simple program?
Post by: Makuto on March 24, 2011, 04:14:04 pm
I got rid of this problem a while ago.  Limiting the frame rate seems to limit the CPU.  I think your hardware might be affecting yours.  Mine only runs like 10% now.
Title: 100% CPU on simple program?
Post by: strongdrink on March 24, 2011, 04:22:36 pm
wurd.... hopefully in 2.0 it will be fixed :S
Title: 100% CPU on simple program?
Post by: Tank on March 24, 2011, 05:27:01 pm
VSync seems to not work with all drivers. I had this problem once on my laptop with an ATI Radeon HD blah-blah-something. SetFramerateLimit() should work in any case.
Title: 100% CPU on simple program?
Post by: strongdrink on March 24, 2011, 11:30:53 pm
Nope! I tried

Code: [Select]

  App.UseVerticalSync (false);
  App.SetFramerateLimit (0x20);


this

Code: [Select]

  App.UseVerticalSync (true);
  App.SetFramerateLimit (0x20);


this

Code: [Select]

  App.SetFramerateLimit (0x20);


and this

Code: [Select]

  App.UseVerticalSync (true);


on 2 differant computers, and it still maxed out the CPU at 100%
Title: 100% CPU on simple program?
Post by: Laurent on March 24, 2011, 11:31:48 pm
Quote
My code is from the documentation files (the first example)

Which one?
Title: 100% CPU on simple program?
Post by: Makuto on March 25, 2011, 02:38:59 am
I fixed this problem a while ago, but it came up again in a different form with different source code.  The window source is below:
Code: [Select]
sf::RenderWindow App(sf::VideoMode(800, 600), "Dodger");
    App.UseVerticalSync(useVertSync);
    App.SetFramerateLimit(frameLimit);

use vert sync is true and framelimit is set to 45.  The program runs on an average amount of CPU, but it acts like a program running on 100% CPU (mouse flickers, disappears when you don't move it, boxes appear by mouse, etc.).  This started occurring all-of-the-sudden.  All of the images move quickly and at a normal pace.
Title: 100% CPU on simple program?
Post by: strongdrink on March 25, 2011, 03:16:50 pm
my code isn't from the tut, it's my own
Title: 100% CPU on simple program?
Post by: Makuto on March 26, 2011, 02:35:14 pm
Mine is now too, but the problem arised after.  To tell you the truth, I only said that I was using the tutorial code so I wouldn't have to post mine, cause I'm lazy... :)
Title: Solution!
Post by: strongdrink on April 08, 2011, 04:17:32 pm
I just stopped thinking about this problem for a while.. but this morning it was really bugging me.. SO! I tried several things... and...

Code: [Select]

    sf::Sleep(0.01);


Doesn't seem to have any effect on speed AND the CPU goes to 0%! :D

w00t!
Title: Re: Solution!
Post by: OniLinkPlus on April 10, 2011, 02:37:48 am
Quote from: "strongdrink"
I just stopped thinking about this problem for a while.. but this morning it was really bugging me.. SO! I tried several things... and...

Code: [Select]

    sf::Sleep(0.01);


Doesn't seem to have any effect on speed AND the CPU goes to 0%! :D

w00t!
Or you could look at the second post in this thread. Your technique is equivalent to SetFramerateLimit(100);
The 100 comes from 1/0.01.
Title: 100% CPU on simple program?
Post by: strongdrink on April 10, 2011, 06:08:51 am
No I tried that, it didn't work either.. I think it might be a hardware problem
Title: 100% CPU on simple program?
Post by: OniLinkPlus on April 10, 2011, 10:26:13 pm
Quote from: "strongdrink"
No I tried that, it didn't work either.. I think it might be a hardware problem
I doubt it. I'm 99% sure that SetFramerateLimit uses sf::Sleep (although I haven't checked, to be honest).
Title: 100% CPU on simple program?
Post by: strongdrink on April 11, 2011, 12:44:16 am
I don't think so... when i do

Code: [Select]

  App.SetFramerateLimit (0x30);


For instance.. it does nothing to my CPU.. but when i do

Code: [Select]

    sf::Sleep(0.01);


it lowers it to 0! it's wacked.
Title: 100% CPU on simple program?
Post by: OniLinkPlus on April 13, 2011, 01:59:28 am
Quote from: "strongdrink"
I don't think so... when i do

Code: [Select]

  App.SetFramerateLimit (0x30);


For instance.. it does nothing to my CPU.. but when i do

Code: [Select]

    sf::Sleep(0.01);


it lowers it to 0! it's wacked.
Why are you using hexadecimal in SetFramerateLimit?

Also, I was correct. This is how SFML handles FramerateLimit:
Code: [Select]
if (myFramerateLimit > 0)

    {

        float remainingTime = 1.f / myFramerateLimit - myClock.GetElapsedTime();

        if (remainingTime > 0)

            Sleep(remainingTime);

    }

In other words, using SetFramerateLimit has the exact same effect as your Sleep method.
Title: 100% CPU on simple program?
Post by: strongdrink on April 13, 2011, 03:09:22 am
i use hex because i believe it compiles faster.. dunno i guess i just like it :D

weiiiirdd... oh well as long as both of them work in the same way it doesn't matter i guess lol