fps - how to divide count by time function to determine fps

前端 未结 3 1105
傲寒
傲寒 2021-02-14 02:36

I have a counter working that counts every frame. what I want to do is divide this by time to determine the FPS of my program. But I\'m not sure how to perform operations on tim

3条回答
  •  无人及你
    2021-02-14 03:23

    Works like a charm

    import time
    import collections
    
    class FPS:
        def __init__(self,avarageof=50):
            self.frametimestamps = collections.deque(maxlen=avarageof)
        def __call__(self):
            self.frametimestamps.append(time.time())
            if(len(self.frametimestamps) > 1):
                return len(self.frametimestamps)/(self.frametimestamps[-1]-self.frametimestamps[0])
            else:
                return 0.0
    fps = FPS()
    for i in range(100):
        time.sleep(0.1)
        print(fps())
    

    Make sure fps is called once per frame

提交回复
热议问题