SFML community forums
Help => Graphics => Topic started by: Pindrought on April 03, 2016, 10:33:37 pm
-
So, when I insert a Sleep(1) command in my loop that is polling/rendering for my window, I get about 100 fps as shown in this image.
(http://i.imgur.com/mAe3kPY.png)
However, when I remove the sleep command, I get about 8,300 FPS as shown in this image.
(http://i.imgur.com/7ngLQEZ.png)
I am trying to understand why putting Sleep(1) takes my fps from 8300 down to 100. I would think that it would take it down to a bit under 1000 if it is normally 8,300 since there are 1,000 miliseconds in one second and Sleep(1) is only sleeping for 1 milisecond.
This is not a serious problem, but I am just curious as to why this happens if anybody knows. I included the Sleep there because I didn't want to put too much strain on the CPU. I didn't think it would reduce the fps by that much though.
Edit (4:56PM) - Well I just tried it again, and now i'm getting around 1000 fps with Sleep(1). It's just strange because after a few tries my fps will go back down to 100 for a few executions and then it will go back to 1000.
(http://i.imgur.com/Y9GW3dX.png)
-
If you want more accuracy in your sleeping duration, you can:
call the timeGetDevCaps function to determine the supported minimum timer resolution and the timeBeginPeriod function to set the timer resolution to its minimum
- Windows' Sleep function (https://msdn.microsoft.com/en-us/library/windows/desktop/ms686298%28v=vs.85%29.aspx),- use sf::Sleep as it attempts to use the highest resolution available,
- loop until a specific amount of time has lapsed.
That final suggestion would be the most accurate but would not but technically sleeping, rather it would be waiting (and somewhat hogging the CPU).
-
Since it may not have been clear from Hapax's post, the key point here is that most sleep() functions are not guaranteed to sleep for exactly the length of time you ask them to. Many of them will effectively act as if they're rounding the time you give them up or down to a multiple of whatever timer resolution the OS supports.
-
Thank you very much both of you for your replies! It makes more sense now.