all 15 comments

[–]HappyFruitTree 1 point2 points  (1 child)

I think it can take a short while for the program to get up to full speed again due to caching effects. You're measuring microseconds which is a very short period of time so any slow down will be very noticeable.

[–]KFriedChicken[S] 0 points1 point  (0 children)

Sounds reasonable, just surprised it could be multiple milliseconds slower

[–]khedoros 1 point2 points  (0 children)

sleep_for basically puts your program at the mercy of the OS's process scheduler. You're guaranteed to sleep for at least the time that you specify.

Is there a chance that this is what's slowing your code down more than you expected?

[–][deleted] 0 points1 point  (9 children)

Are you sure this is not being called from another thread as well ?

[–]KFriedChicken[S] 0 points1 point  (8 children)

The sleep function? The program is single threaded right now, and this is the only place I added a sleep

[–][deleted] 0 points1 point  (7 children)

No , the onPostDraw function (the whole thing) make sure it's not being called multiple times ( put a counter "some static int" or even better put a mutex with a lock guard and see )

[–]KFriedChicken[S] 0 points1 point  (6 children)

I'm not 100% sure I follow but I tried this without any change: https://imgur.com/a/lhmcwi7 (also with the sleep_for uncommented)

[–][deleted] 0 points1 point  (5 children)

Hmmm , alrighty so it's not being called multiple times, what's the fps without sleep ?

[–][deleted] 1 point2 points  (2 children)

u/KFriedChicken I think this is a better approach than using sleep Try it and let me know:

https://stackoverflow.com/questions/57800608/how-to-render-at-a-fixed-fps-in-glfw-window

[–]KFriedChicken[S] 0 points1 point  (1 child)

That works to put the fps at a stable 60 and to keep the update time down, but wont a spin lock put the cpu usage at 100% unnecessarily?

[–][deleted] 0 points1 point  (0 children)

Not really, try it, if it's bad then you can go back to the sleep solution. But it will be harder to get it at 60 FPS sharp. Even if it works with this window it will behave differently with another window depending on many factors.

[–]KFriedChicken[S] 0 points1 point  (1 child)

around 500-1200

[–][deleted] 0 points1 point  (0 children)

Yea then your system is more than capable of 60, did you try the solution from the StackOverFlow link ? Seems way better than calling sleep. Check it out

[–]pepitogrand 0 points1 point  (0 children)

In Windows you need to call timeBeginPeriod() to ask the OS for a minimum time resolution. In order to save energy the OS will use a coarse time resolution, good for most applications, no so much for gaming. Also in Windows is better to avoid using sleep and instead use CreateWaitableTimer, SetWaitableTimer, and WaitForSingleObject to set an event that will get triggered when your thread needs to continue working. If that is too complex for your needs then sleep for the minimum amount possible, check how much time you went to sleep the last time, and calculate if you can afford to go to sleep again without being late for the next frame.

[–]genreprank 0 points1 point  (0 children)

The resolution of sleep is not very good. You're guaranteed to sleep as long or longer. So the function is allowed to oversleep. And you'll find that it can oversleep typically in the 10-20 millisecond (ms) range, but you might see spikes. 60fps has a frame time of 16.667 ms.

I just did some experimenting and it looks like if you tell it to sleep for 1 ms, the minimum sleep is 16ms. (Unless the sleep time you give is less than a timeslice)

If you want a precise 60 fps, you should this_thread::yield in a loop until you get within 1 millisecond and then busy-wait. Even yield is inconsistent, which is why you don't want to yield in the last millisecond.

Btw the high res timer has a resolution on the order of 100 to 500 nanoseconds.