我想创建一个python函数来测试每个函数花费的时间,并用它的时间打印它的名字,如何打印函数名称,如果还有其他方法,请告诉我
def measureTime(a):
start = time.clock()
a()
elapsed = time.clock()
elapsed = elapsed - start
print "Time spent in (function name) is: ", elapsed
Run Code Online (Sandbox Code Playgroud) 我目前正在评估不同的python绘图库.现在我正在尝试使用matplotlib,我对性能非常失望.以下示例是从SciPy示例中修改的,并且每秒仅给出~8帧!
有什么方法可以加快速度,或者我应该选择不同的绘图库?
from pylab import *
import time
ion()
fig = figure()
ax1 = fig.add_subplot(611)
ax2 = fig.add_subplot(612)
ax3 = fig.add_subplot(613)
ax4 = fig.add_subplot(614)
ax5 = fig.add_subplot(615)
ax6 = fig.add_subplot(616)
x = arange(0,2*pi,0.01)
y = sin(x)
line1, = ax1.plot(x, y, 'r-')
line2, = ax2.plot(x, y, 'g-')
line3, = ax3.plot(x, y, 'y-')
line4, = ax4.plot(x, y, 'm-')
line5, = ax5.plot(x, y, 'k-')
line6, = ax6.plot(x, y, 'p-')
# turn off interactive plotting - speeds things up by 1 Frame …Run Code Online (Sandbox Code Playgroud) 我想通过调用imshowfor循环来显示一个简单的动画.这是我的问题的演示:
import pylab,time
images = [pylab.uniform(0,255,(50,50)) for _ in xrange(40)]
pylab.ion()
timings = []
for img in images:
tic = time.time()
pylab.imshow(img)
pylab.draw()
toc = time.time()
timings.append(toc-tic)
pylab.clf()
pylab.plot(timings)
pylab.title('elapsed time per iteration')
pylab.ioff()
pylab.show()
Run Code Online (Sandbox Code Playgroud)
请注意,我在运行循环之前生成图像,并且我唯一的部分是imshow和draw函数.我得到的结果如下:

我怎样才能避免这种放缓?