Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 19 additions & 31 deletions memory_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ def memory_usage(proc= -1, num= -1, interval=.1):

if str(proc).endswith('.py'):
filename = _find_script(proc)
f = open(filename, 'r')
proc = f.read()
f.close()
with open(filename) as f:
proc = f.read()
# TODO: make sure script's directory is on sys.path
def f_exec(x, locals):
# function interface for exec
Expand Down Expand Up @@ -249,36 +248,25 @@ def show_results(prof, stream=None):
stream.write(template % (l, mem, inc, line))

if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser(usage=_CMD_USAGE)
parser.add_option('-o', '--outfile', dest='outfile',
help='Save stats to <outfile>', default=None)
parser.add_option('-v', '--visualize', action='store_true',
dest='visualize', help='Visualize result at exit',
default=True)
parser.add_option('-l', '--line', action='store_true',
dest='line', help='Do line-by-line timings',
default=True)

from argparse import ArgumentParser
parser = ArgumentParser(usage=_CMD_USAGE)
parser.add_argument('filename', help='The file to profile')

if not sys.argv[1:] or sys.argv[1] in ("--help", "-h"):
parser.print_help()
sys.exit(2)

(options, args) = parser.parse_args()
sys.argv[:] = args

if options.line:
prof = LineProfiler()
__file__ = _find_script(args[0])
if sys.version_info[0] < 3:
import __builtin__
__builtin__.__dict__['profile'] = prof
execfile(__file__, locals(), locals())
else:
import builtins
builtins.__dict__['profile'] = prof
exec(compile(open(__file__).read(), __file__, 'exec'), locals(), globals())

if options.visualize:
show_results(prof)
args = parser.parse_args()

prof = LineProfiler()
__file__ = _find_script(args.filename)
if sys.version_info[0] < 3:
import __builtin__
__builtin__.__dict__['profile'] = prof
execfile(__file__, locals(), locals())
else:
import builtins
builtins.__dict__['profile'] = prof
exec(compile(open(__file__).read(), __file__, 'exec'), locals(), globals())

show_results(prof)