本文整理匯總了Python中line_profiler.LineProfiler方法的典型用法代碼示例。如果您正苦於以下問題:Python line_profiler.LineProfiler方法的具體用法?Python line_profiler.LineProfiler怎麽用?Python line_profiler.LineProfiler使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類line_profiler
的用法示例。
在下文中一共展示了line_profiler.LineProfiler方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: activate_profiler
# 需要導入模塊: import line_profiler [as 別名]
# 或者: from line_profiler import LineProfiler [as 別名]
def activate_profiler():
if sys.version_info[0] == 3: # PY3
import builtins
else:
import __builtin__ as builtins
if Options()['misc'].get('profile', False):
# if profiler is activated, associate line_profiler
Logger()('Activating line_profiler...')
try:
import line_profiler
except ModuleNotFoundError:
Logger()('Failed to import line_profiler.', log_level=Logger.ERROR, raise_error=False)
Logger()('Please install it from https://github.com/rkern/line_profiler', log_level=Logger.ERROR, raise_error=False)
return
prof = line_profiler.LineProfiler()
builtins.__dict__['profile'] = prof
else:
# otherwise, create a blank profiler, to disable profiling code
builtins.__dict__['profile'] = lambda func: func
prof = None
return prof
示例2: reset
# 需要導入模塊: import line_profiler [as 別名]
# 或者: from line_profiler import LineProfiler [as 別名]
def reset(self):
functions = self.functions
line_prof = line_profiler.LineProfiler()
# copy settings
if self._enable:
line_prof.enable()
else:
line_prof.disable()
if self._enable_by_count:
line_prof.enable_by_count()
else:
line_prof.disable_by_count()
# add previously registered functions
for fxn in functions:
line_prof.add_function(fxn)
self._line_prof = line_prof
return self
示例3: _profile
# 需要導入模塊: import line_profiler [as 別名]
# 或者: from line_profiler import LineProfiler [as 別名]
def _profile(prof, statement, glob, loc):
"""Profile a Python statement."""
dir = Path('.profile')
ensure_dir_exists(dir)
prof.runctx(statement, glob, loc)
# Capture stdout.
old_stdout = sys.stdout
sys.stdout = output = StringIO()
try: # pragma: no cover
from line_profiler import LineProfiler
if isinstance(prof, LineProfiler):
prof.print_stats()
else:
prof.print_stats('cumulative')
except ImportError: # pragma: no cover
prof.print_stats('cumulative')
sys.stdout = old_stdout
stats = output.getvalue()
# Stop capture.
if 'Line' in prof.__class__.__name__: # pragma: no cover
fn = 'lstats.txt'
else:
fn = 'stats.txt'
stats_file = dir / fn
stats_file.write_text(stats)
示例4: get_profiler
# 需要導入模塊: import line_profiler [as 別名]
# 或者: from line_profiler import LineProfiler [as 別名]
def get_profiler():
if not profiling:
return
# lazy load (this won't be available in production)
import line_profiler
glob = globals()
if 'line_profiler_' not in glob:
profiler = line_profiler.LineProfiler()
if profile_by_count:
profiler.enable_by_count()
glob['line_profiler_'] = profiler
print 'initialized profiler'
return glob['line_profiler_']
示例5: test_WignerDRecursion_lineprofiling
# 需要導入模塊: import line_profiler [as 別名]
# 或者: from line_profiler import LineProfiler [as 別名]
def test_WignerDRecursion_lineprofiling():
from line_profiler import LineProfiler
ell_max = 8
hcalc = HCalculator(ell_max)
cosβ = 2*np.random.rand(100, 100) - 1
workspace = hcalc.workspace(cosβ)
hcalc(cosβ, workspace=workspace) # Run once to ensure everything is compiled
profiler = LineProfiler(hcalc.__call__)#, _step_2, _step_3, _step_4, _step_5, _step_6)
profiler.runctx('hcalc(cosβ, workspace=workspace)', {'hcalc': hcalc, 'cosβ': cosβ, 'workspace': workspace}, {})
print()
profiler.print_stats()
示例6: profile_each_line
# 需要導入模塊: import line_profiler [as 別名]
# 或者: from line_profiler import LineProfiler [as 別名]
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
示例7: profile_linebyline
# 需要導入模塊: import line_profiler [as 別名]
# 或者: from line_profiler import LineProfiler [as 別名]
def profile_linebyline(func):
import line_profiler
@functools.wraps(func)
def wrapper(*args, **kwargs):
prof = line_profiler.LineProfiler()
val = prof(func)(*args, **kwargs)
prof.print_stats()
return val
return wrapper
# Some debug testing here
示例8: profile
# 需要導入模塊: import line_profiler [as 別名]
# 或者: from line_profiler import LineProfiler [as 別名]
def profile(title):
def wrapper(f):
def printProfile(*args):
lp = LineProfiler()
dec_f = lp(f)
output_value = dec_f(*args)
print("Line Profile for:",title)
print("----------------------")
lp.print_stats()
return output_value
return printProfile
return wrapper
##############################################################
示例9: enable_profiler
# 需要導入模塊: import line_profiler [as 別名]
# 或者: from line_profiler import LineProfiler [as 別名]
def enable_profiler(env, scope):
# decorate line profiler
import line_profiler
import inspect
env.profile_deco = profile_deco = line_profiler.LineProfiler()
for name in scope:
obj = scope[name]
if getattr(obj, "__module__", None) != "rqalpha.user_module":
continue
if inspect.isfunction(obj):
scope[name] = profile_deco(obj)
if inspect.isclass(obj):
for key, val in six.iteritems(obj.__dict__):
if inspect.isfunction(val):
setattr(obj, key, profile_deco(val))
示例10: start_line_profiler
# 需要導入模塊: import line_profiler [as 別名]
# 或者: from line_profiler import LineProfiler [as 別名]
def start_line_profiler():
"""Start the line profiler"""
global _active_line_profiler
_active_line_profiler = line_profiler.LineProfiler()
xlcAlert("Line Profiler Active\n"
"Run the function you are interested in and then stop the profiler.\n"
"Ensure you have decoratored the function with @enable_line_profiler.")
示例11: __init__
# 需要導入模塊: import line_profiler [as 別名]
# 或者: from line_profiler import LineProfiler [as 別名]
def __init__(self):
self._line_prof = line_profiler.LineProfiler()
示例12: _enable_profiler
# 需要導入模塊: import line_profiler [as 別名]
# 或者: from line_profiler import LineProfiler [as 別名]
def _enable_profiler(line_by_line=False): # pragma: no cover
"""Enable the profiler."""
if 'profile' in builtins.__dict__:
return builtins.__dict__['profile']
if line_by_line:
import line_profiler
prof = line_profiler.LineProfiler()
else:
prof = ContextualProfile()
builtins.__dict__['profile'] = prof
return prof
示例13: do_profile
# 需要導入模塊: import line_profiler [as 別名]
# 或者: from line_profiler import LineProfiler [as 別名]
def do_profile(follow=None):
"""
使用line_profiler創建性能分析裝飾器
follow列表選擇要追蹤的函數,如果為空,則全部分析
用例:
def num_range(n):
for x in range(n):
yield x
@do_profile(follow=[num_range])
def expensive_function():
for x in num_range(1000):
_ = x ** x
return 'OK!'
result = expensive_function()
"""
if follow is None:
follow = list()
def inner(func):
def profiled_func(*args, **kwargs):
profiler = LineProfiler()
try:
profiler.add_function(func)
for f in follow:
profiler.add_function(f)
profiler.enable_by_count()
return func(*args, **kwargs)
finally:
profiler.print_stats()
return profiled_func
return inner
示例14: main
# 需要導入模塊: import line_profiler [as 別名]
# 或者: from line_profiler import LineProfiler [as 別名]
def main():
profiler = LineProfiler()
for f in funcs_to_profile:
profiler.add_function(f)
profiler.wrap_function(run)()
profiler.print_stats(stripzeros=True)
示例15: do_profile
# 需要導入模塊: import line_profiler [as 別名]
# 或者: from line_profiler import LineProfiler [as 別名]
def do_profile(follow=(), follow_all_methods=False):
"""
Decorator to profile a function or class method
It uses line_profiler to give detailed reports on time spent on each
line in the code.
Pros: has intuitive and finely detailed reports. Can follow
functions in third party libraries.
Cons:
has external dependency on line_profiler and is quite slow,
so don't use it for benchmarking.
Handy tip:
Just decorate your test function or class method and pass any
additional problem function(s) in the follow argument!
If any follow argument is a string, it is assumed that the string
refers to bound a method of the class
See also
--------
do_cprofile, test_do_profile
"""
def inner(func):
def profiled_func(*args, **kwargs):
try:
profiler = LineProfiler()
profiler.add_function(func)
if follow_all_methods:
cls = args[0] # class instance
_add_all_class_methods(profiler, cls,
except_=func.__name__)
for f in follow:
_add_function_or_classmethod(profiler, f, args)
profiler.enable_by_count()
return func(*args, **kwargs)
finally:
profiler.print_stats()
return profiled_func
return inner