Summary Args: freq (TYPE): Description
(freq=1)
| 6 | |
| 7 | |
| 8 | def time_it(freq=1): |
| 9 | """Summary |
| 10 | |
| 11 | Args: |
| 12 | freq (TYPE): Description |
| 13 | """ |
| 14 | class time_cls(object): |
| 15 | |
| 16 | """Summary |
| 17 | |
| 18 | Attributes: |
| 19 | acc_time (float): Description |
| 20 | call_count (int): Description |
| 21 | class_name (str): Description |
| 22 | freq (TYPE): Description |
| 23 | func (TYPE): Description |
| 24 | """ |
| 25 | |
| 26 | def __init__(self, func): |
| 27 | """Summary |
| 28 | |
| 29 | Args: |
| 30 | func (TYPE): Description |
| 31 | """ |
| 32 | # wraps(func)(self) |
| 33 | self.__name__ = func.__name__ |
| 34 | self.__module__ = func.__module__ |
| 35 | self.__doc__ = func.__doc__ |
| 36 | self.func = func |
| 37 | self.class_name = "" |
| 38 | |
| 39 | # used to calculate average run time |
| 40 | self.call_count = 0 |
| 41 | self.acc_time = 0.0 |
| 42 | self.freq = freq |
| 43 | |
| 44 | def __call__(self, *args, **kwargs): |
| 45 | """Summary |
| 46 | |
| 47 | Args: |
| 48 | *args (TYPE): Description |
| 49 | **kwargs (TYPE): Description |
| 50 | |
| 51 | Returns: |
| 52 | TYPE: Description |
| 53 | """ |
| 54 | start = time.time() |
| 55 | # args contains the caller itself |
| 56 | # print args[0] and you will see |
| 57 | result = self.func(*args, **kwargs) |
| 58 | end = time.time() |
| 59 | |
| 60 | self.call_count += 1 |
| 61 | self.acc_time += (end - start) |
| 62 | if self.call_count > 0 and self.call_count % self.freq == 0: |
| 63 | print("Avg time cost of %s%s is %f", |
| 64 | self.class_name, self.__name__, |
| 65 | self.acc_time / self.call_count) |
nothing calls this directly
no outgoing calls
no test coverage detected