-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache-class-function-call.py
More file actions
50 lines (32 loc) · 1.02 KB
/
cache-class-function-call.py
File metadata and controls
50 lines (32 loc) · 1.02 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
import time
import click
class AClass:
@classmethod
def sum(cls, i_max):
r_sum = 0
for i in range(i_max // 2):
r_sum += 1 + i_max
return r_sum
# return (1 + i_max) * i_max / 2
@click.group()
def main():
pass
@main.command('test', help='run benchmark')
@click.argument('loop', type=int, default=10000)
@click.argument('count', type=int, default=10)
@click.argument('cache', type=int, default=0)
def test(**kwargs):
if kwargs['cache']:
setattr(AClass, 'sum', AClass.sum)
benchmark_times = []
for count_i in range(kwargs['count']):
start_time = time.time()
for i in range(kwargs['loop']):
AClass.sum(i)
end_time = time.time()
benchmark_times.append(end_time - start_time)
print('run {0} benchmark, max time {1}, min time {2}, avg time {3}'.format(
kwargs['loop'], max(benchmark_times), min(benchmark_times),
sum(benchmark_times) / len(benchmark_times)))
if __name__ == '__main__':
main()