I have created a simple snake clone and would like to execute game logic every 100ms while rendering as fast as possible. How can I achieve this when the program might run with very different frame rates?

For example when compiling the code to a native executable I have over 1000 where I can just wait until 100ms are passed before executing the next logic step. But when compiling to asm.js I get frame rates of about 20 which means the period between two logic steps could be everything from 100ms to 149ms which would result in visible stutter (one movement is faster than another).

  • \$\begingroup\$ Have you researched fixed timesteps and variable timesteps? \$\endgroup\$
    – House
    Dec 7, 2015 at 18:18
  • 2
    \$\begingroup\$ gafferongames.com/game-physics/fix-your-timestep \$\endgroup\$
    – jmegaffin
    Dec 7, 2015 at 18:21
  • \$\begingroup\$ You can use the InvokeRepeating to trigger function and repeat it on a special rate \$\endgroup\$
    – Sata
    Dec 11, 2015 at 8:40
  • \$\begingroup\$ InvokeRepeating seams to be a feature Unity or maby C# but I'm using C. Thanks nevertheless :) \$\endgroup\$
    – ooxi
    Dec 11, 2015 at 8:44
  • \$\begingroup\$ Why would you want to render faster than your update step? If nothing has changed, why update the screen? \$\endgroup\$
    – Niels
    Feb 29, 2016 at 11:57

1 Answer 1


This is some basic example of calculation of the framerate.
I'm assuming that there's a function called current_milliseconds() that returns the number of milliseconds from the beginning of the program.

/* Before the loop */
int time = current_milliseconds(); // Setting up the Timer
int FPS = 60;

/* Inside the loop (at the bottom) */
int frame_ms = current_milliseconds() - time; // Milliseconds the frame took to render
// Calculating the number of frames the machine could render in 1 second at that speed
FPS = 1000.0 / (frame_ms <= 0 ? 1 : frame_ms);
time = current_milliseconds();

My advice from personal experience: do NOT limit framerate manually. It's better for you to check the time of rendering of each frame and adjust your game logic accordingly.
Another solution is to use vSync to get a constant framerate.

  • \$\begingroup\$ "It's better for you to check the time of rendering of each frame and adjust your game logic accordingly" - this depends sensitively on the criteria used to define "better." If one of your criteria is deterministic behaviour to minimize synchonization between multiple clients, or if you want consistent gameplay across a wide range of devices with varying performance, then running game logic at a variable framerate is not necessarily better, and can in fact make it harder to meet these goals. \$\endgroup\$
    – DMGregory
    Dec 7, 2015 at 19:45
  • \$\begingroup\$ @DMGregory I was just saying that capping the framerate manually with delays is awful and won't get you the result you want, so or you use some library with framerate capping or you use vsync, if you need fixed framerate \$\endgroup\$
    – Xriuk
    Dec 8, 2015 at 9:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .