MCPcopy Create free account
hub / github.com/alibaba/bigcomputing / time_cls

Class time_cls

DIEN/wrap_time.py:14–86  ·  view source on GitHub ↗

Summary Attributes: acc_time (float): Description call_count (int): Description class_name (str): Description freq (TYPE): Description func (TYPE): Description

Source from the content-addressed store, hash-verified

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)
66 self.call_count = 0
67 self.acc_time = 0.0
68
69 return result
70
71 def __get__(self, instance, cls):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected