LoginSignup
4
3

More than 5 years have passed since last update.

pythonの簡易FPS計測

Posted at

Pythonプログラムのループ処理の速度を知りたい.プロファイラを使うのが確実な方法だが,手抜きをして簡易にFPS(frame per second)を表示したい.

fps.py
import time, sys
import Queue
import numpy as np

class fpsCalculator(object):
    def __init__(self, length=5):
        self.times = Queue.LifoQueue()
        self.length = length
        self.fpsHist = []

    def tick_tack(self):
        if self.times.qsize() < self.length:
            self.times.put(time.time())

        elif self.times.qsize() == self.length:
            begin = self.times.get()
            end = time.time()
            self.times.put(end)
            fps = self.length / (end - begin)
            sys.stdout.write("\r FPS: %2.2f " % fps)
            sys.stdout.flush()

            self.fpsHist.append(fps)

    def getFPS(self):
        return np.average(np.array(self.fpsHist[1:]))

fps = fpsCalculator()

while True:
    try:
        fps.tick_tack()
        time.sleep(0.1)
    except KeyboardInterrupt:
        break

print 'average of fps = ', fps.getFPS()

しかしこれはfps=60にはならない.

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3