分析简单python脚本
我曾尝试使用分析简单python脚本,python,performance,cprofile,Python,Performance,Cprofile,我曾尝试使用cProfile评测python应用程序。因此,我编写了一个简单的脚本,下面是我得到的结果 def foo(): for i in range(100000): print i def bar(): for i in range(100): print "bar"*i foo() bar() 当我以python-m profile script.py的形式运行上述脚本时,我得到以下输出: 7 function calls in 0.13
cProfile
评测python应用程序。因此,我编写了一个简单的脚本,下面是我得到的结果
def foo():
for i in range(100000):
print i
def bar():
for i in range(100):
print "bar"*i
foo()
bar()
当我以python-m profile script.py
的形式运行上述脚本时,我得到以下输出:
7 function calls in 0.136 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
2 0.000 0.000 0.000 0.000 :0(range)
1 0.000 0.000 0.000 0.000 :0(setprofile)
1 0.000 0.000 0.136 0.136 lip.py:1(<module>)
1 0.136 0.136 0.136 0.136 lip.py:1(foo)
1 0.000 0.000 0.000 0.000 lip.py:5(bar)
1 0.000 0.000 0.136 0.136 profile:0(<code object <module> at 0x7fae5a978a30, file "lip.py", line 1>)
0 0.000 0.000 profile:0(profiler)
在0.136秒内调用7次函数
订购人:标准名称
ncalls tottime percall cumtime percall文件名:lineno(函数)
2 0.000 0.000 0.000 0.000:0(范围)
1 0.000 0.000 0.000 0.000:0(设置配置文件)
1 0.000 0.000 0.136 0.136唇。py:1()
1 0.1360.1360.1360.136唇。py:1(foo)
1 0.000 0.000 0.000 0.000唇。py:5(巴)
1 0.000 0.000 0.136 0.136配置文件:0(<0x7fae5a978a30处的代码对象模块,文件“lip.py”,第1行>)
0.000 0.000配置文件:0(配置文件器)
但从ouptut来看,似乎只有方法
foo
消耗了0.136 s,而方法bar
消耗了0.00 s。这是为什么?您在foo
中比在bar
中多做了1000倍的工作
假设它们具有相同的速度,0.136/1000=0.000136
,这对于此显示来说太小了,而bar()
的时间刚好转到0.00