1 | from time import time |
||
2 | from decimal import Decimal |
||
3 | |||
4 | _TIMES = [] |
||
5 | |||
6 | |||
7 | def now(): |
||
8 | """Capture the current time.""" |
||
9 | return time() |
||
10 | |||
11 | |||
12 | def rounder(exact, decimals=2): |
||
13 | """Round a float to a certain number of decimal places.""" |
||
14 | return round(Decimal(exact), decimals) |
||
15 | |||
16 | |||
17 | def human_time(runtime, decimals=2): |
||
18 | """Display runtime in a human friendly format.""" |
||
19 | if runtime < 1: |
||
20 | return str('mms: ' + str('{:f}'.format(rounder(runtime * 1000, decimals)))) |
||
21 | elif runtime < 60: |
||
22 | return str('sec: ' + str('{:f}'.format(rounder(runtime, decimals)))) |
||
23 | else: |
||
24 | return str('min: ' + str('{:f}'.format(rounder(runtime / 60, decimals)))) |
||
25 | |||
26 | |||
27 | View Code Duplication | class Timer: |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
28 | def __init__(self, msg=None, decimal_places=2): |
||
29 | self.msg = msg |
||
30 | self._decimal_places = decimal_places |
||
31 | |||
32 | self.start = now() |
||
33 | self.elapsed_str = None |
||
34 | self.elapsed_float = None |
||
35 | |||
36 | def __str__(self): |
||
37 | return str(self.end) |
||
38 | |||
39 | def __float__(self): |
||
40 | return float(self.elapsed_float) |
||
41 | |||
42 | def __enter__(self): |
||
43 | return self |
||
44 | |||
45 | def __exit__(self, exc_type, exc_val, exc_tb): |
||
46 | if self.msg: |
||
47 | print(self.msg, self.end) |
||
48 | else: |
||
49 | print(self.end) |
||
50 | |||
51 | @property |
||
52 | def end(self): |
||
53 | # Calculate run time |
||
54 | return human_time(now() - self.start, self._decimal_places) |
||
55 | |||
56 | @staticmethod |
||
57 | def decorator(func): |
||
58 | """A function timer decorator.""" |
||
59 | def function_timer(*args, **kwargs): |
||
60 | """A nested function for timing other functions.""" |
||
61 | # Capture start time |
||
62 | start = now() |
||
63 | |||
64 | # Execute function with arguments |
||
65 | value = func(*args, **kwargs) |
||
66 | |||
67 | # Calculate run time |
||
68 | runtime = human_time(now() - start) |
||
69 | print('{func:50} --> {time}'.format(func=func.__qualname__, time=runtime)) |
||
70 | _TIMES.append((func.__qualname__, runtime)) |
||
71 | return value |
||
72 | |||
73 | return function_timer |
||
74 | |||
75 | @staticmethod |
||
76 | def decorator_noprint(func): |
||
77 | """A function timer decorator.""" |
||
78 | def function_timer(*args, **kwargs): |
||
79 | """A nested function for timing other functions.""" |
||
80 | # Capture start time |
||
81 | start = now() |
||
82 | |||
83 | # Execute function with arguments |
||
84 | value = func(*args, **kwargs) |
||
85 | |||
86 | # Calculate run time |
||
87 | runtime = human_time(now() - start) |
||
88 | _TIMES.append((func.__qualname__, runtime)) |
||
89 | return value |
||
90 | |||
91 | return function_timer |
||
92 | |||
93 | @property |
||
94 | def times(self): |
||
95 | return _TIMES |
||
96 | |||
97 | @staticmethod |
||
98 | def print_times(class_name=None): |
||
99 | for index, ft in enumerate( |
||
100 | sorted([(func, t) for func, t in Timer().times if class_name and func.startswith(class_name)], |
||
101 | key=lambda e: e[1])): |
||
102 | func, t = ft |
||
103 | print('{0}.) {1:35} --> {2}'.format(index, func, t)) |
||
104 |