Python用の高レベルのプロファイリングモジュールはありますか?
-
04-10-2019 - |
質問
Pythonコードをプロファイルしたいです。私はよく知っています cProfile
, 、そして私はそれを使用しますが、それはあまりにも低レベルです。 (たとえば、プロファイリングする関数から戻り値をキャッチする簡単な方法さえありません。)
私がやりたいことの1つ:私は自分のプログラムで機能を取り、プロファイルするように設定したい 急いで プログラムを実行している間。
たとえば、機能があるとしましょう heavy_func
私のプログラムで。プログラムを開始して持ちたいです heavy_func
プロファイル自体ではなく機能します。しかし、私のプログラムの実行時のいつか、私は変えたいです heavy_func
実行中にそれ自体をプロファイルします。 (プログラムの実行中に物を操作する方法を疑問に思っている場合は、デバッグプローブまたはGUIアプリに統合されているシェルからそれを行うことができます。)
このようなものをしているモジュールはすでに書かれていますか?自分で書くことはできますが、前に尋ねたかったので、車輪を再発明しません。
解決 2
私はそれのために自分のモジュールを書きました。私はそれを呼んだ cute_profile
. これがコードです. これがテストです.
それはの一部です garlicsim, 、だからあなたがそれを使用したい場合、あなたは インストール garlicsim
そして、やります from garlicsim.general_misc import cute_profile
.
Python 3コードで使用する場合は、 Python 3フォーク garlicsim
.
これが次のとおりです 時代遅れ コードからの抜粋:
import functools
from garlicsim.general_misc import decorator_tools
from . import base_profile
def profile_ready(condition=None, off_after=True, sort=2):
'''
Decorator for setting a function to be ready for profiling.
For example:
@profile_ready()
def f(x, y):
do_something_long_and_complicated()
The advantages of this over regular `cProfile` are:
1. It doesn't interfere with the function's return value.
2. You can set the function to be profiled *when* you want, on the fly.
How can you set the function to be profiled? There are a few ways:
You can set `f.profiling_on=True` for the function to be profiled on the
next call. It will only be profiled once, unless you set
`f.off_after=False`, and then it will be profiled every time until you set
`f.profiling_on=False`.
You can also set `f.condition`. You set it to a condition function taking
as arguments the decorated function and any arguments (positional and
keyword) that were given to the decorated function. If the condition
function returns `True`, profiling will be on for this function call,
`f.condition` will be reset to `None` afterwards, and profiling will be
turned off afterwards as well. (Unless, again, `f.off_after` is set to
`False`.)
`sort` is an `int` specifying which column the results will be sorted by.
'''
def decorator(function):
def inner(function_, *args, **kwargs):
if decorated_function.condition is not None:
if decorated_function.condition is True or \
decorated_function.condition(
decorated_function.original_function,
*args,
**kwargs
):
decorated_function.profiling_on = True
if decorated_function.profiling_on:
if decorated_function.off_after:
decorated_function.profiling_on = False
decorated_function.condition = None
# This line puts it in locals, weird:
decorated_function.original_function
base_profile.runctx(
'result = '
'decorated_function.original_function(*args, **kwargs)',
globals(), locals(), sort=decorated_function.sort
)
return locals()['result']
else: # decorated_function.profiling_on is False
return decorated_function.original_function(*args, **kwargs)
decorated_function = decorator_tools.decorator(inner, function)
decorated_function.original_function = function
decorated_function.profiling_on = None
decorated_function.condition = condition
decorated_function.off_after = off_after
decorated_function.sort = sort
return decorated_function
return decorator
所属していません StackOverflow