-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSylTimer.py
More file actions
67 lines (52 loc) · 1.73 KB
/
SylTimer.py
File metadata and controls
67 lines (52 loc) · 1.73 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# =================================================================
# [GPL 3.0 License]
# Copyright (C) 2023 by CLiF and Syeosle
# See https://github.com/CLiF-1593/SortingVisualizer/blob/master/LICENSE for more details.
# =================================================================
import time
class SylTimer:
def __init__(self):
self.start = None
self.passed = 0
def clock(self, sec: float):
if self.start is None:
self.start = time.time()
while time.time() - self.start - sec < self.passed:
pass
self.passed += sec
def zero(self):
self.passed = 0
self.start = time.time()
if __name__ == '__main__':
delay = 0.0000005
t_len = 60
repeat = int(t_len / delay)
e_flag = False
print(f'delay = {delay * 1000} ms\ntest length = {t_len} sec\ntotal calls = {repeat}')
s = SylTimer()
print('\nstart clock')
for _ in range(5):
s.zero()
t = time.time()
for _ in range(repeat):
if time.time() - t > 2 * t_len:
print('ERROR IS LONGER THAN TIME LENGTH')
e_flag = True
break
s.clock(delay)
du = time.time() - t
if not e_flag: print(f'E(ms) = {round((du - t_len) * 1000, 3)}')
e_flag = False
#
'''print('\nstart sleep')
for _ in range(5):
t = time.time()
for _ in range(repeat):
if time.time() - t > 2 * t_len:
print('ERROR IS LONGER THAN TIME LENGTH')
e_flag = True
break
time.sleep(delay)
du = time.time() - t
if not e_flag: print(f'E(ms) = {round((du - t_len) * 1000, 3)}')
e_flag = False'''