# Frame duration varies by a millisecond every few frames

When I run the program at 60 fps, I get 0.016 seconds per frame most of the time, then 0.017 s once every few frames. A similar thing happens for 30 fps, varying between 0.033 and 0.034 seconds per frame.

Should my framerate and frame duration be stable? Even when nothing different is happening in each frame?

while (m_bRunning)
{
m_Timer.Start();

m_frameStart = SDL_GetTicks();

m_frameCount += 1;

HandleEvent(e);

Update();

Draw();

m_Timer.Pause();

if(m_frameDelay >= m_Timer.GetTicks())
{
SDL_Delay(m_frameDelay - m_Timer.GetTicks());
}

m_deltaTime = (double(SDL_GetTicks() - m_frameStart)/1000.f);

return m_bRunning;
}

• Note that this is exactly what you'd expect to see, since 1000 milliseconds does not divide evenly into either 30 or 60. So to run at 60 fps consistently, you can't always have 16 ms frames (too short - after 60 of them you've passed only 0.96 of a second) or 17 ms frames (too long - 60 of those would put you over 1.02 seconds), you have to have about two thirds of your frames 17 ms long and the other third 16 ms - or all frames at a fractional 16.3333333333333333333... etc. Apr 20, 2023 at 15:43
• thanks, you must be onto something aye. because 0.96 and 1.02 are the numbers i get so am i doing it right? by right i mean is this passable for a game to use that doesnt need to run for days or anything like that Apr 22, 2023 at 3:38
• It's unclear to me what problem you're concerned about here. Apr 22, 2023 at 3:40
• you say its what i should expect im asking if that means its right Apr 22, 2023 at 3:57

If you are using V-Sync in the renderer via flag SDL_RENDERER_PRESENTVSYNC then the 0.001 difference is nothing you can control because of how communication between GPU and CPU works. However, if you are halting your loop with a Sleep() function to get to 0.016s, assuming you do your calculations correctly, you should know that any Sleep() call is never accurate. I.e. Sleep(1 : milliseconds) is never guaranteed to last exactly 1 millisecond because of how kernels and their task scheduling works. When you call Sleep() your cpu does some other work in another place, unrelated to your process (it can take more than 1ms for it to resume your process).