MCPcopy Index your code
hub / github.com/pythonprofilers/memory_profiler / LineProfiler

Class LineProfiler

memory_profiler.py:699–850  ·  view source on GitHub ↗

A profiler that records the amount of memory for each line

Source from the content-addressed store, hash-verified

697
698
699class LineProfiler(object):
700 """ A profiler that records the amount of memory for each line """
701
702 def __init__(self, **kw):
703 include_children = kw.get('include_children', False)
704 backend = kw.get('backend', 'psutil')
705 self.code_map = CodeMap(
706 include_children=include_children, backend=backend)
707 self.enable_count = 0
708 self.max_mem = kw.get('max_mem', None)
709 self.prevlines = []
710 self.backend = choose_backend(kw.get('backend', None))
711 self.prev_lineno = None
712
713 def __call__(self, func=None, precision=1):
714 if func is not None:
715 self.add_function(func)
716 f = self.wrap_function(func)
717 f.__module__ = func.__module__
718 f.__name__ = func.__name__
719 f.__doc__ = func.__doc__
720 f.__dict__.update(getattr(func, '__dict__', {}))
721 return f
722 else:
723 def inner_partial(f):
724 return self.__call__(f, precision=precision)
725
726 return inner_partial
727
728 def add_function(self, func):
729 """ Record line profiling information for the given Python function.
730 """
731 try:
732 # func_code does not exist in Python3
733 code = func.__code__
734 except AttributeError:
735 warnings.warn("Could not extract a code object for the object %r"
736 % func)
737 else:
738 self.code_map.add(code)
739
740 @contextmanager
741 def _count_ctxmgr(self):
742 self.enable_by_count()
743 try:
744 yield
745 finally:
746 self.disable_by_count()
747
748 def wrap_function(self, func):
749 """ Wrap a function to profile it.
750 """
751
752 if iscoroutinefunction(func):
753 @coroutine
754 def f(*args, **kwargs):
755 with self._count_ctxmgr():
756 res = yield from func(*args, **kwargs)

Callers 6

mprunMethod · 0.90
memory_profiler.pyFile · 0.90
test_loop_countMethod · 0.90
test_normal_incrMethod · 0.90
test_loop_incrMethod · 0.90
test_decrMethod · 0.90

Calls

no outgoing calls

Tested by 4

test_loop_countMethod · 0.72
test_normal_incrMethod · 0.72
test_loop_incrMethod · 0.72
test_decrMethod · 0.72