本文整理汇总了Python中line_profiler.LineProfiler类的典型用法代码示例。如果您正苦于以下问题:Python LineProfiler类的具体用法?Python LineProfiler怎么用?Python LineProfiler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了LineProfiler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: profile_lines
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: ProfilingPanel
class ProfilingPanel(Panel):
"""
Panel that displays profiling information.
"""
title = _('Profiling')
template = 'debug_toolbar_line_profiler/panels/profiling.html'
def _unwrap_closure_and_profile(self, func):
if not hasattr(func, '__code__'):
return
self.line_profiler.add_function(func)
for subfunc in getattr(func, 'profile_additional', []):
self._unwrap_closure_and_profile(subfunc)
if func.__closure__:
for cell in func.__closure__:
target = cell.cell_contents
if inspect.isclass(target) and View in inspect.getmro(target):
for name, value in inspect.getmembers(target):
if name[0] != '_' and inspect.ismethod(value):
self._unwrap_closure_and_profile(value)
else:
self._unwrap_closure_and_profile(target)
def process_view(self, request, view_func, view_args, view_kwargs):
self.profiler = cProfile.Profile()
args = (request,) + view_args
self.line_profiler = LineProfiler()
self._unwrap_closure_and_profile(view_func)
self.line_profiler.enable_by_count()
out = self.profiler.runcall(view_func, *args, **view_kwargs)
self.line_profiler.disable_by_count()
return out
def add_node(self, func_list, func, max_depth, cum_time=0.1):
func_list.append(func)
func.has_subfuncs = False
if func.depth < max_depth:
for subfunc in func.subfuncs():
if (subfunc.stats[3] >= cum_time or
(hasattr(self.stats, 'line_stats') and
(subfunc.func in self.stats.line_stats.timings))):
func.has_subfuncs = True
self.add_node(func_list, subfunc, max_depth, cum_time=cum_time)
def process_response(self, request, response):
if not hasattr(self, 'profiler'):
return None
# Could be delayed until the panel content is requested (perf. optim.)
self.profiler.create_stats()
self.stats = DjangoDebugToolbarStats(self.profiler)
self.stats.line_stats = self.line_profiler.get_stats()
self.stats.calc_callees()
root = FunctionCall(self.stats, self.stats.get_root_func(), depth=0)
func_list = []
self.add_node(func_list, root, 10, root.stats[3] / 8)
self.record_stats({'func_list': func_list})
示例3: profile_each_line
def profile_each_line(func, *args, **kwargs):
profiler = LineProfiler()
profiled_func = profiler(func)
retval = None
try:
retval = profiled_func(*args, **kwargs)
finally:
profiler.print_stats()
return retval
示例4: profiled_func
def profiled_func(*args, **kwargs):
try:
profiler = LineProfiler()
profiler.add_function(func)
for f in follow:
profiler.add_function(f)
profiler.enable_by_count()
return func(*args, **kwargs)
finally:
profiler.print_stats()
示例5: wrapper
def wrapper(*args, **kwargs):
# Don't profile if debugging is off (PROD server mode)
if DEBUG:
logger.error('Line Profiling (@profile_this_by_line) ' + func.__name__ + '() to ' + stats_filename + '.')
profiler = LineProfiler()
profiled_func = profiler(func)
try:
retval = profiled_func(*args, **kwargs)
finally:
# profiler.print_stats()
profiler.dump_stats(stats_filename)
else:
logger.error('Line Profiling (@profile_this_by_line) attempted on ' + func.__name__ + '() while in production mode. Profiling Bypassed.')
retval = func(*args, **kwargs)
return retval
示例6: profiled_func
def profiled_func(*args, **kwargs):
try:
profiler = LineProfiler()
profiler.add_function(func)
for f in follow:
if isinstance(f, basestring):
f = to_function(f)
profiler.add_function(f)
profiler.enable_by_count()
return func(*args, **kwargs)
finally:
profiler.print_stats()
示例7: __init__
def __init__(self, *args):
self.profile = LineProfiler()
if len(args) > 0:
for func in args:
if callable(func):
self.add_function(func)
示例8: profile
def profile(algo, data=None, to_profile=[]):
""" Profile algorithm using line_profiler.
:param algo: Algorithm instance.
:param data: Stock prices, default is random portfolio.
:param to_profile: List of methods to profile, default is `step` method.
Example of use:
tools.profile(Anticor(window=30, c_version=False), to_profile=[Anticor.weights])
"""
from line_profiler import LineProfiler
if data is None:
data = random_portfolio(n=1000, k=10, mu=0.)
to_profile = to_profile or [algo.step]
profile = LineProfiler(*to_profile)
profile.runcall(algo.run, data)
profile.print_stats()
示例9: process_view
def process_view(self, request, view_func, view_args, view_kwargs):
self.profiler = cProfile.Profile()
args = (request,) + view_args
self.line_profiler = LineProfiler()
self._unwrap_closure_and_profile(view_func)
self.line_profiler.enable_by_count()
out = self.profiler.runcall(view_func, *args, **view_kwargs)
self.line_profiler.disable_by_count()
return out
示例10: profile_list_deserialization
def profile_list_deserialization(serializer, child_serializer, data_list):
if os.environ.get('CI', None) != 'true' or not LineProfiler:
return
profile = LineProfiler(serializer.to_internal_value, child_serializer.to_internal_value)
profile.enable()
serializer.to_internal_value(data_list)
profile.disable()
profile.print_stats()
示例11: profiled_func
def profiled_func(*args, **kwargs):
try:
lp = LineProfiler()
lp.add_function(f)
lp.enable_by_count()
return f(*args, **kwargs)
finally:
lp.print_stats()
示例12: profile_list_serialization
def profile_list_serialization(serializer, child_serializer, instances_list):
if os.environ.get('CI', None) != 'true' or not LineProfiler:
return
profile = LineProfiler(serializer.instances_list, child_serializer.instances_list)
profile.enable()
serializer.to_representation(instances_list)
profile.disable()
profile.print_stats()
示例13: DataReader_bin_test2
def DataReader_bin_test2(self):
self.select_file(num=1)
prf = LineProfiler()
prf.add_function(self.read_bin_file_to_tx2)
prf.runcall(self.read_bin_file_to_tx2, start=3 * 10**7, datapoints=10**6)
prf.print_stats()
print(len(self.x), math.log10((len(self.x))))
self.plot_timecorse_of_move(show_it=1)
示例14: main
def main():
profiler = cProfile.Profile()
profiler.enable()
function_runner('original_method')
function_runner('step_one')
function_runner('step_two')
function_runner('step_three')
function_runner('step_four')
function_runner('step_five')
function_runner('step_six')
function_runner('step_seven')
function_runner('step_eight')
function_runner('step_nine')
function_runner('current')
profiler.disable()
profiler.dump_stats('function_event.stats')
line_profiler = LineProfiler(CurrentFunctionContainer().current)
line_profiler.enable()
function_runner('current')
line_profiler.disable()
line_profiler.dump_stats('function_event.line_stats')
line_profiler.print_stats()
print 'Original', timeit.timeit(
lambda: function_runner('original_method'), number=7)
print 'One', timeit.timeit(
lambda: function_runner('step_one'), number=7)
print 'Two', timeit.timeit(
lambda: function_runner('step_two'), number=7)
print 'Three', timeit.timeit(
lambda: function_runner('step_three'), number=7)
print 'Four', timeit.timeit(
lambda: function_runner('step_four'), number=7)
print 'Five', timeit.timeit(
lambda: function_runner('step_five'), number=7)
print 'Six', timeit.timeit(
lambda: function_runner('step_six'), number=7)
print 'Seven', timeit.timeit(
lambda: function_runner('step_seven'), number=7)
print 'Eight', timeit.timeit(
lambda: function_runner('step_eight'), number=7)
print 'Nine', timeit.timeit(
lambda: function_runner('step_nine'), number=7)
print 'Current', timeit.timeit(
lambda: function_runner('current'), number=7)
示例15: benchmark
def benchmark(cls, func, *args):
from line_profiler import LineProfiler
prf = LineProfiler()
prf.add_function(func)
ret = prf.runcall(func, *args)
prf.print_stats()
return ret