有 python.sleep 随计算时间变化
Have python.sleep variate with computation time
对于一个动画项目,我们想要为可变数量的对象建模。这意味着计算和渲染部分将花费可变的计算时间。但是我们希望动画的帧率保持不变。所以我们想计算一段代码花费了多少时间,如果它小于预期的帧速率,我们在计算下一帧之前等待剩余时间。
有没有简单的方法来做到这一点?
一种方法是使用具有时间方法的时间库,该方法允许您计算一段代码执行所花费的时间,下面是一个示例。
import time
start_time = time.time()
#Your code here
end_time = time.time() - start_time
print(end_time)
你秒表时间,然后休眠 1-x 秒。
sleep_time = 1/1 #Second number is frames per unit of time, first number is unit of time. In seconds.
from time import time #Important function: time(): Secs since unix epoch
while True: #Init animation
init_time = time() #Init stopwatch
#Code here Do your code
timer = time()-init_time #End stopwatch and remember time in a variable.
while time() < init_time+sleep_time-timer: pass #Wait ___ seconds
#Restart and go to next frame.
如果您输入的代码持续时间超过 'sleep time',那么它不会等待。
希望这对您有所帮助,祝您制作动画好运!
对于一个动画项目,我们想要为可变数量的对象建模。这意味着计算和渲染部分将花费可变的计算时间。但是我们希望动画的帧率保持不变。所以我们想计算一段代码花费了多少时间,如果它小于预期的帧速率,我们在计算下一帧之前等待剩余时间。
有没有简单的方法来做到这一点?
一种方法是使用具有时间方法的时间库,该方法允许您计算一段代码执行所花费的时间,下面是一个示例。
import time
start_time = time.time()
#Your code here
end_time = time.time() - start_time
print(end_time)
你秒表时间,然后休眠 1-x 秒。
sleep_time = 1/1 #Second number is frames per unit of time, first number is unit of time. In seconds.
from time import time #Important function: time(): Secs since unix epoch
while True: #Init animation
init_time = time() #Init stopwatch
#Code here Do your code
timer = time()-init_time #End stopwatch and remember time in a variable.
while time() < init_time+sleep_time-timer: pass #Wait ___ seconds
#Restart and go to next frame.
如果您输入的代码持续时间超过 'sleep time',那么它不会等待。
希望这对您有所帮助,祝您制作动画好运!