Search code examples
pythonanimationsleep

Have python.sleep variate with computation time


For an animation project, we want to model a variable number of objects. This means that the computation and rendering part will take variable computation time. But we want the frame rate of the animation to remain constant. So we would like to compute how much time a section of code has taken, and if it is less then the expected frame rate, we wait the remainded before calculation the next frame.

Is there an easy way to do this?


Solution

  • One way to do it is by using the time library that has a method time that allows you to count how much time a section of code has taken to execute, an example down below.

    import time
    start_time = time.time()
    #Your code here
    end_time = time.time() - start_time
    print(end_time)