Skip to content

Commit

Permalink
Add function decorator for non-script usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
dnouri committed Nov 1, 2012
1 parent b2c807c commit 4ae4c90
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.rst
Expand Up @@ -68,6 +68,20 @@ Python interpreter after that line has been executed. The third column
with respect to the last one. The last column (*Line Contents*) prints with respect to the last one. The last column (*Line Contents*) prints
the code that has been profiled. the code that has been profiled.


Decorator
=========

A function decorator is also available. Use as follows::

from memory_profiler import profile

@profile
def my_func():
a = [1] * (10 ** 6)
b = [2] * (2 * 10 ** 7)
del b
return a

Setting debugger breakpoints Setting debugger breakpoints
============================= =============================
It is possible to set breakpoints depending on the amount of memory used. It is possible to set breakpoints depending on the amount of memory used.
Expand Down
9 changes: 9 additions & 0 deletions memory_profiler.py
Expand Up @@ -538,6 +538,15 @@ def magic_memit(self, line=''):
print('ERROR: could not read memory usage, try with a lower interval or more iterations') print('ERROR: could not read memory usage, try with a lower interval or more iterations')




def profile(func):
def wrapper(*args, **kwargs):
prof = LineProfiler()
val = prof(func)(*args, **kwargs)
show_results(prof)
return val
return wrapper


if __name__ == '__main__': if __name__ == '__main__':
from optparse import OptionParser from optparse import OptionParser
parser = OptionParser(usage=_CMD_USAGE, version=__version__) parser = OptionParser(usage=_CMD_USAGE, version=__version__)
Expand Down

0 comments on commit 4ae4c90

Please sign in to comment.