-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathCountsPerSec.py
More file actions
36 lines (28 loc) · 1.27 KB
/
CountsPerSec.py
File metadata and controls
36 lines (28 loc) · 1.27 KB
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
# Original Code: https://github.com/nrsyed/computer-vision/blob/master/multithread/CountsPerSec.py
# Modified for use in PyPotter
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from datetime import datetime
class CountsPerSec:
"""
Class that tracks the number of occurrences ("counts") of an
arbitrary event and returns the frequency in occurrences
(counts) per second. The caller must increment the count.
"""
def __init__(self):
self._SmoothingFactor = 90
self._timeList = []
def countsPerSec(self):
self._timeList.append(datetime.now())
if (len(self._timeList) > self._SmoothingFactor):
self._timeList.pop(0)
elapsed_time = (self._timeList[-1] - self._timeList[0]).total_seconds()
if (elapsed_time > 0):
return len(self._timeList) / elapsed_time
return 0