0
|
1 """calculates your updates-per-second"""
|
|
2
|
|
3 import time
|
|
4
|
|
5 class Updatefreq:
|
|
6 """make one of these, call update() on it as much as you want,
|
|
7 and then float() or str() the object to learn the updates per second.
|
|
8
|
|
9 the samples param to __init__ specifies how many past updates will
|
|
10 be stored. """
|
|
11
|
|
12 def __init__(self,samples=20):
|
|
13 self.times=[0]
|
|
14 self.samples=samples
|
|
15
|
|
16 def update(self):
|
|
17
|
|
18 """call this every time you do an update"""
|
|
19 self.times=self.times[-self.samples:]
|
|
20 self.times.append(time.time())
|
|
21
|
|
22 def __float__(self):
|
|
23
|
|
24 """a cheap algorithm, for now, which looks at the first and
|
|
25 last times only"""
|
|
26
|
|
27 try:
|
|
28 hz=len(self.times)/(self.times[-1]-self.times[0])
|
|
29 except ZeroDivisionError:
|
|
30 return 0
|
|
31 return hz
|
|
32 def __str__(self):
|
|
33 return "%.2fHz"%float(self)
|