-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfps.py
38 lines (30 loc) · 1.14 KB
/
fps.py
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
30
31
32
33
34
35
36
37
38
# *****************************************************************************
# * Author: Miguel Magalhaes
# * Email: [email protected]
# *****************************************************************************
# * FPS
# *****************************************************************************
import datetime
class FPS:
def __init__(self):
self._start = None
self._last = None
self._end = None
self._numFrames = 0
def start(self):
self._start = datetime.datetime.now()
self._last = self._start
return self
def stop(self):
self._end = datetime.datetime.now()
def update(self):
current_fps = 1 / (datetime.datetime.now() - self._last).total_seconds()
self._last = datetime.datetime.now()
self._numFrames += 1
return current_fps
def elapsed(self):
# return the total number of seconds between the start and end interval
return (self._end - self._start).total_seconds()
def fps(self):
# compute the (approximate) frames per second
return self._numFrames / self.elapsed()