-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFPSCounter.py
More file actions
29 lines (25 loc) · 813 Bytes
/
FPSCounter.py
File metadata and controls
29 lines (25 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from collections import deque
import time
class FPSCounter:
"""
Class for counting fps. Averages the last n frame times.
usage counter = FPSCounter(). Then call fps=counter() one time per frame
"""
def __init__(self, length: int):
"""
Constructor for fps counter
Args:
length: fps over the last length frames
"""
self.time_counter = deque(maxlen=length)
def __call__(self):
"""
Call the counter to add another frame time.
Returns:
FPS average of the last length frame
"""
self.time_counter.append(time.time())
if len(self.time_counter) > 1:
return len(self.time_counter) / (self.time_counter[-1] - self.time_counter[0])
else:
return 0.0