-
Notifications
You must be signed in to change notification settings - Fork 14.4k
/
repeat_and_time.py
154 lines (119 loc) · 3.74 KB
/
repeat_and_time.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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations
import contextlib
import functools
import math
import random
import signal
import time
class TimingResult:
"""Timing result."""
def __init__(self):
self.start_time = 0
self.end_time = 0
self.value = 0
@contextlib.contextmanager
def timing(repeat_count: int = 1):
"""
Measures code execution time.
:param repeat_count: If passed, the result will be divided by the value.
"""
result = TimingResult()
result.start_time = time.monotonic()
try:
yield result
finally:
end_time = time.monotonic()
diff = (end_time - result.start_time) * 1000.0
result.end_time = end_time
if repeat_count == 1:
result.value = diff
print(f"Loop time: {diff:.3f} ms")
else:
average_time = diff / repeat_count
result.value = average_time
print(f"Average time: {average_time:.3f} ms")
def repeat(repeat_count=5):
"""
Function decorators that repeat function many times.
:param repeat_count: The repeat count
"""
def repeat_decorator(f):
@functools.wraps(f)
def wrap(*args, **kwargs):
last_result = None
for _ in range(repeat_count):
last_result = f(*args, **kwargs)
return last_result
return wrap
return repeat_decorator
class TimeoutException(Exception):
"""Exception when the test timeo uts"""
@contextlib.contextmanager
def timeout(seconds=1):
"""
Executes code only limited seconds. If the code does not end during this time, it will be interrupted.
:param seconds: Number of seconds
"""
def handle_timeout(signum, frame):
raise TimeoutException("Process timed out.")
try:
signal.signal(signal.SIGALRM, handle_timeout)
signal.alarm(seconds)
except ValueError:
raise Exception("timeout can't be used in the current context")
try:
yield
except TimeoutException:
print("Process timed out.")
finally:
try:
signal.alarm(0)
except ValueError:
raise Exception("timeout can't be used in the current context")
if __name__ == "__main__":
def monte_carlo(total=10000):
"""Monte Carlo"""
inside = 0
for _ in range(0, total):
x_val = random.random() ** 2
y_val = random.random() ** 2
if math.sqrt(x_val + y_val) < 1:
inside += 1
return (inside / total) * 4
# Example 1:s
with timeout(1):
print("Sleep 5s with 1s timeout")
time.sleep(4)
print(":-/")
print()
# Example 2:
REPEAT_COUNT = 5
@timing(REPEAT_COUNT)
@repeat(REPEAT_COUNT)
@timing()
def get_pi():
"""Returns PI value:"""
return monte_carlo()
res = get_pi()
print("PI: ", res)
print()
# Example 3:
with timing():
res = monte_carlo()
print("PI: ", res)