Python脚本中每行的日志执行时间?
我有一个Python脚本,它从第一行到最后一行用简单的逻辑执行,非常简单。脚本在不同环境的两台机器上的性能非常不同,所以我试图找出哪一行代码给我带来了困难 我看过一些(Python脚本中每行的日志执行时间?,python,python-2.7,profiling,Python,Python 2.7,Profiling,我有一个Python脚本,它从第一行到最后一行用简单的逻辑执行,非常简单。脚本在不同环境的两台机器上的性能非常不同,所以我试图找出哪一行代码给我带来了困难 我看过一些(timeit)关于记录整个函数的执行时间的文章。然而,我感兴趣的是找出Python在执行脚本中的每一行代码上花费的时间 源代码: import math result = pow(2,5) newlist = [] newlist.append(result) print newlist 我想要得到的(以秒为单位执行的行时间数)
timeit
)关于记录整个函数的执行时间的文章。然而,我感兴趣的是找出Python在执行脚本中的每一行代码上花费的时间
源代码:
import math
result = pow(2,5)
newlist = []
newlist.append(result)
print newlist
我想要得到的(以秒为单位执行的行时间数):
编辑:我试图使用一个可用的标准库,但收到一条错误消息
我运行的源代码:
import hotshot
import hotshot.stats
logfile = r"C:\testlog.prof"
prof_obj = hotshot.Profile(logfile,lineevents=True,linetimings=False)
prof_obj.start()
a = 1000
b = 2000
c = a + b
print c
prof_obj.stop()
prof_obj.close()
stats_obj = hotshot.stats.load(logfile) #getting error on this line *
stats_obj.strip_dirs()
stats_obj.sort_stats('time', 'calls')
stats_obj.print_stats(20)
编辑:我从Python基本参考书中找到了另一个使用hotshot的例子
import hotshot
import hotshot.stats
def function_to_call():
print "AA"
print "BB"
logfile = r"C:\testlog.prof"
prof_obj = hotshot.Profile(logfile,lineevents=True,linetimings=True)
prof_obj.runcall(function_to_call)
prof_obj.close()
stats_obj = hotshot.stats.load(logfile)
stats_obj.sort_stats('time', 'calls')
stats_obj.print_stats()
但是,这并没有给我任何关于每行代码执行的信息,只提供了每个函数调用的信息:
5 function calls in 0.012 seconds
Ordered by: internal time, call count
ncalls tottime percall cumtime percall filename:lineno(function)
4 0.012 0.003 0.012 0.003 <string>:6(write)
1 0.000 0.000 0.012 0.012 c:\gis\temp\simple_hotshot.py:11(function_to_call)
0 0.000 0.000 profile:0(profiler)
在0.012秒内调用5次函数
排序人:内部时间、呼叫计数
ncalls tottime percall cumtime percall文件名:lineno(函数)
4 0.012 0.003 0.012 0.003:6(写入)
1 0.000 0.000 0.012 0.012 c:\gis\temp\simple\u hotshot.py:11(函数调用)
0.000 0.000配置文件:0(配置文件器)
有一种方法可以满足您的需求。它也有很好的装饰器,你可以把你想要的功能放在一行一行的层次上。你可以看这些文件
也来看看。看起来您可以设置linetimings
参数来获取所需内容。不过,我不确定将来的版本是否会支持hotshot
。有一种方法可以满足您的需要。它也有很好的装饰器,你可以把你想要的功能放在一行一行的层次上。你可以看这些文件
也来看看。看起来您可以设置
linetimings
参数来获取所需内容。不过,我不确定hotshot
是否会在未来的版本中得到支持。很好。核心Python中包括了什么?“我很有可能无法在其中一台机器上安装任何东西。”阿列克斯特列申科夫补充了更多信息。我认为你可以通过hotshot得到你想要的,它内置于Python中。让我知道你的情况。非常感谢。我已经尝试过了,并用代码和错误消息更新了我的答案。在互联网上很难找到hotshot的任何示例用法,我只看到了官方Python文档附带的一个。你能指出我上面的代码有什么问题吗?很好。核心Python中包括了什么?“我很有可能无法在其中一台机器上安装任何东西。”阿列克斯特列申科夫补充了更多信息。我认为你可以通过hotshot得到你想要的,它内置于Python中。让我知道你的情况。非常感谢。我已经尝试过了,并用代码和错误消息更新了我的答案。在互联网上很难找到hotshot的任何示例用法,我只看到了官方Python文档附带的一个。你能指出我上面的代码出了什么问题吗?
import hotshot
import hotshot.stats
def function_to_call():
print "AA"
print "BB"
logfile = r"C:\testlog.prof"
prof_obj = hotshot.Profile(logfile,lineevents=True,linetimings=True)
prof_obj.runcall(function_to_call)
prof_obj.close()
stats_obj = hotshot.stats.load(logfile)
stats_obj.sort_stats('time', 'calls')
stats_obj.print_stats()
5 function calls in 0.012 seconds
Ordered by: internal time, call count
ncalls tottime percall cumtime percall filename:lineno(function)
4 0.012 0.003 0.012 0.003 <string>:6(write)
1 0.000 0.000 0.012 0.012 c:\gis\temp\simple_hotshot.py:11(function_to_call)
0 0.000 0.000 profile:0(profiler)