def wrapper(*args, **kwargs): global _profile_out, _profile_struct, \ _profile_funcs_dict, _profile_start ovr = etime() if _profile_start is not None: if self.name is None: try: name = fn.__self__.pathname except AttributeError: name = "<%s>" % args[0].__class__.__name__ name = '.'.join((name, fn.__name__)) self.name = name else: name = self.name stack = _profile_dec._call_stack if stack: caller = stack[-1] else: caller = '' stack.append(name) path = ','.join(stack) if path not in _profile_funcs_dict: # save the id for this path _profile_funcs_dict[path] = len(_profile_funcs_dict) start = etime() ret = fn(*args[1:], **kwargs) end = etime() stack.pop() _profile_struct.t = end - start _profile_struct.ovr = start - ovr # keep track of overhead for later subtraction _profile_struct.tstamp = start _profile_struct.id = _profile_funcs_dict[path] _profile_out.write(_profile_struct) return ret else: return fn(*args[1:], **kwargs)
def stop(): """Turn off profiling. """ global _profile_total, _profile_start if _profile_start is None: return _profile_total += (etime() - _profile_start) _profile_start = None
def start(): """Turn on profiling. """ global _profile_start if _profile_start is not None: print("profiling is already active.") return _profile_start = etime()