本文整理汇总了Python中line_profiler.LineProfiler.runctx方法的典型用法代码示例。如果您正苦于以下问题:Python LineProfiler.runctx方法的具体用法?Python LineProfiler.runctx怎么用?Python LineProfiler.runctx使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类line_profiler.LineProfiler
的用法示例。
在下文中一共展示了LineProfiler.runctx方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: profile_lines
# 需要导入模块: from line_profiler import LineProfiler [as 别名]
# 或者: from line_profiler.LineProfiler import runctx [as 别名]
def profile_lines(self, functions, statement):
from line_profiler import LineProfiler
import __builtin__
profile = LineProfiler(*functions)
# Add the profiler to the builtins for @profile..
# will probably not work for all modules in ecoControl,
# as they are already imported before and @profile will then throw an error
if 'profile' in __builtin__.__dict__:
had_profile = True
old_profile = __builtin__.__dict__['profile']
else:
had_profile = False
old_profile = None
__builtin__.__dict__['profile'] = profile
try:
try:
profile.runctx(statement, globals(), locals())
message = ''
except SystemExit:
message = """*** SystemExit exception caught in code being profiled."""
except KeyboardInterrupt:
message = ("*** KeyboardInterrupt exception caught in code being "
"profiled.")
finally:
if had_profile:
__builtin__.__dict__['profile'] = old_profile
# Trap text output.
stdout_trap = StringIO()
profile.print_stats(stdout_trap)
output = stdout_trap.getvalue()
output = output.rstrip()
pfile = open("profile.txt", 'a')
pfile.write("\n\n" + 20 * "=" + "*********====================== profile at time " +
time.strftime("%b %d %Y %H:%M:%S", time.gmtime(time.time())) +
"==================================\n\n")
pfile.write(output)
pfile.close()
print '\n*** Profile printout saved to text file profile.txt', message
示例2: LineProfiler
# 需要导入模块: from line_profiler import LineProfiler [as 别名]
# 或者: from line_profiler.LineProfiler import runctx [as 别名]
#!/usr/bin/env python
import sys
from line_profiler import LineProfiler
if __name__ == '__main__':
filename = sys.argv[1]
profiler = LineProfiler()
profiler.runctx(
'execfile(%r, globals())' % (filename,), locals(), locals())
profiler.print_stats()