Simple FPS measurement of python

I want to know the speed of loop processing of a Python program. The surest way is to use a profiler, but I want to cut corners and easily display 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()

However, this does not result in fps = 60.

Recommended Posts

Simple FPS measurement of python
Introduction of Python
python time measurement
Basics of Python ①
Basics of python ①
Copy of python
Introduction of Python
Automating simple tasks with Python Table of contents
Simple comparison of Python libraries that operate Excel
List of python modules
Simple gRPC in Python
Unification of Python environment
Copy of python preferences
Basics of Python scraping basics
[python] behavior of argmax
Usage of Python locals ()
the zen of Python
Modify Maya Python FPS
Python --Simple security software
# 4 [python] Basics of functions
Basic knowledge of Python
Sober trivia of python3
Summary of Python arguments
Measurement of execution time
Selection of measurement data
Basics of python: Output
Installation of matplotlib (Python 3.3.2)
Application of Python 3 vars
Various processing of Python
Python --Simple multi-thread sample
Implementation example of simple LISP processing system (Python version)
[Python] [Word] [python-docx] Simple analysis of diff data using python
Calculate the regression coefficient of simple regression analysis with python
[Python] Correct usage of map
Towards the retirement of Python2
Python execution time measurement memo
Use Measurement Protocol in Python
Summary of Python3 list operations
A simple sample of pivot_table.
Python --Quick start of logging
Recommendation of binpacking library of python
[python] Value of function object (?)
Automatic update of Python module
Python --Check type of values
Static analysis of Python programs
About various encodings of Python 3
Equivalence of objects in Python
Introduction of activities applying Python
python> Handling of 2D arrays
Install multiple versions of Python
Version upgrade of python Anaconda
Handling of python on mac
python: Basics of using scikit-learn ①
# 1 [python3] Simple calculation using variables
2.x, 3.x character code of python
Comparison of 4 Python web frameworks
Execution time measurement with Python With
Simple simulation of virus infection
Check OpenSSL version of python 2.6
[With simple explanation] Scratch implementation of deep Boltzmann machine with Python ②
Python implementation of particle filters