Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7063211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:41:13+00:00 2026-05-28T04:41:13+00:00

i want my program to do a task every x frames. However it does

  • 0

i want my program to do a task every x frames. However it does not seem to work, due to the fact that the timer in python seems to be inaccurate. How do i get this piece of code to obey my set framerate?

import time
fps = 5
skipticks = 1/(fps*1.0)
i= 0
nextsnap=time.clock()
print skipticks, fps
while (True):
    tim= time.clock()
    i=i+1
    # this prints the fps
    #'print 'Fps at start',i, 1/(time.time()-tim)
    # this is the sleep that limits the fps
    nextsnap+=skipticks
    sleeptime = nextsnap-time.clock()
    if (sleeptime>0):
        time.sleep (sleeptime)
    else:
        print 'took too long'
    print 'Fps at end:#', i, 1/(time.clock()-tim)

this produces on my computer:

 Fps at end:# 45 4.36627853079
Fps at end:# 46 6.44119324776
Fps at end:# 47 4.53966049676
Fps at end:# 48 4.66471670624
Fps at end:# 49 7.18312473536
Fps at end:# 50 4.34786490268
Fps at end:# 51 6.5263951487
Fps at end:# 52 4.71715853908
Fps at end:# 53 4.59636712435
Fps at end:# 54 6.87201830723
Fps at end:# 55 4.31062740848

Why are there frames that are rendered too fast? And why is the fps count inaccurate?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:41:14+00:00Added an answer on May 28, 2026 at 4:41 am

    If it’s about mesuring the accuracy you can have with time.clock() and time.sleep(), use something as simple as possible like:

    import time
    
    fps = 5
    time_delta = 1./fps
    
    while True:
        t0 = time.clock()
        time.sleep(time_delta)
        t1 = time.clock()
        print 1. / (t1 - t0)
    

    What is your OS and what is the result of this simple mesure ?

    If it’s about cleaning your code, I would separate more clearly the FPS evaluation, the processing and the sleep call. Here is an example (the loop #0 frequency displayed should be ignored).

    import time
    import random
    from itertools import count
    
    fps = 5
    loop_delta = 1./fps
    
    current_time = target_time = time.clock()
    for i in count():
        #### loop frequency evaluation
        previous_time, current_time = current_time, time.clock()
        time_delta = current_time - previous_time
        print 'loop #%d frequency: %s' % (i, 1. / time_delta)
    
        #### processing
        # processing example that sleeps a random time between 0 and loop_delta/2.
        time.sleep(random.uniform(0, loop_delta / 2.))
    
        #### sleep management
        target_time += loop_delta
        sleep_time = target_time - time.clock()
        if sleep_time > 0:
            time.sleep(sleep_time)
        else:
            print 'took too long'
    

    Also, you probably want time.time() rather than time.clock(). See time.clock() vs. time.time() – accuracy answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is very simple task I want to do, that somehow makes the program
I have a C# program that I want to run automatically every night without
I want to create a program that does remote installs. I'd like to be
I'm making a task-based program that needs to have plugins. Tasks need to have
Got a task to make a program that registers animals and the object is
I've got the log file for a program; the log records every task done
Every variation on the following code that I try doesn't work - whether DoSomething()
I want my program to change its window's caption in response to various user
I want to program a board game (similar to checkers) for the iPhone. Would
I want to program something in both C# and F#. I have partial classes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.