【发布时间】:2014-03-03 11:56:29
【问题描述】:
我想获得探查器的统计信息,而不是按调用次数和花费的时间,而是按自然执行顺序排序和分组。
例如,如果我们将 cProfiler 用于下一个简单程序:
def my_sleep0():
sleep( 0.05 )
def my_sleep1():
sleep( 0.15 )
my_sleep2()
def my_sleep2():
my_sleep3()
def my_sleep3():
sleep( 0.1 )
if __name__ == '__main__':
p = Profiler( './' ) # a wrapper for cProfiler, cProfiler starts here
i = 10
for i in range( 1, 5 ):
i += 100
my_sleep0()
my_sleep1()
my_sleep1()
# cProfiler destructs and ends here
我想要一个按自然执行顺序的统计,例如:
Ordered by: execution
calling_function cumtime backtrace
test.py:36(my_sleep0) <cumtime> test.py:43()
test.py:39(my_sleep1) <cumtime> test.py:44()
test.py:43(my_sleep2) <cumtime> test.py:20 called by test.py:44(my_sleep1)
test.py:43(my_sleep3) <cumtime> test.py:25 called by test.py:20(my_sleep1)->test.py:26(my_sleep2)
test.py:39(my_sleep1) <cumtime> test.py:45()
test.py:43(my_sleep2) <cumtime> test.py:20 called by test.py:25(my_sleep1)
test.py:43(my_sleep3) <cumtime> test.py:25 called by test.py:20(my_sleep1)->test.py:26(my_sleep2)
...
(<cumtime> 是一个数字时间。)
Python中有没有办法实现它?怎么样?
加法。
我的 Profiler() 代码现在不这样做:
class Profiler:
def __init__( self, dir ):
self._profiler = cProfile.Profile()
self._profiler.enable()
self._dir = dir # profiler output is "/tmp/profiler/{key}/<files_here>"
def stop( self ):
profilerDir = self._dir
self._profiler.disable()
with open( os.path.join( profilerDir, datetime.now().strftime( "%d-%m-%y %H.%M.%S" ) ), 'w' ) as f:
stat = pstats.Stats( self._profiler, stream = f ).sort_stats( 'pcalls' )
stat.print_stats()
stat.print_callers()
stat.print_callees()
def __del__( self ):
self.stop()
【问题讨论】:
-
@KillianDS 好的。但似乎 cProfiler 没有该功能。我想知道是否有工具或方法可以实现我想要的。
-
可以按行排序。为什么要按(第一次?)执行时间排序?这对你有什么好处?
-
@KillianDS 我想按执行顺序排序,而不是按执行时间排序。对不起。
-
是的,这就是我的意思,我仍然不明白你为什么要这样做(
why通常比how更重要的信息,以便最终得到正确的回答)。 -
@KillianDS 我很难弄清楚
some是从哪里调用的。我想轻松地将探查器的统计信息与真实代码链接起来。这就像你在某个动作之前测量时间,然后在某个动作之后测量时间,然后将信息保存到一个统计文件中:时间差和当前调用堆栈。并且仅按此顺序 - 执行顺序。
标签: python python-2.7 profiling profiler