python優化迴圈 Python 優化迴圈

2021-10-18 14:42:04 字數 2314 閱讀 6809

我希望優化一些由兩個巢狀迴圈組成的

python**.我對numpy並不那麼熟悉,但據我所知,它應該能夠幫助我提高這項任務的效率.下面是我編寫的測試**,它反映了實際**中發生的情況.目前使用numpy範圍和迭代器比通常的python更慢.我究竟做錯了什麼?這個問題的最佳解決方案是什麼?

謝謝你的幫助!

import numpy

import time

# setup a problem analagous to that in the real code

npoints_per_plane = 1000

nplanes = 64

naxis = 1000

npoints3d = naxis + npoints_per_plane * nplanes

npoints = naxis + npoints_per_plane

specres = 1000

sol = dict()

sol["ems"] = numpy.zeros(npoints3d)

sol["abs"] = numpy.zeros(npoints3d)

# this would normally be non-random input data

data = dict()

data["ems"] = numpy.zeros((npoints,specres))

data["abs"] = numpy.zeros((npoints,specres))

for ip in range(npoints):

data["ems"][ip,:] = numpy.random.random(specres)[:]

data["abs"][ip,:] = numpy.random.random(specres)[:]

ems_mod = numpy.random.random(1)[0]

abs_mod = numpy.random.random(1)[0]

ispec = numpy.random.randint(specres)

# this the code i want to optimize

t0 = time.time()

# usual python range and iterator

for ip in range(npoints_per_plane):

jp = naxis + ip

for ipl in range(nplanes):

ip3d = jp + npoints_per_plane * ipl

sol["ems"][ip3d] = data["ems"][jp,ispec] * ems_mod

sol["abs"][ip3d] = data["abs"][jp,ispec] * abs_mod

t1 = time.time()

# numpy ranges and iterator

ip_vals = numpy.arange(npoints_per_plane)

ipl_vals = numpy.arange(nplanes)

for ip in numpy.nditer(ip_vals):

jp = naxis + ip

for ipl in numpy.nditer(ipl_vals):

ip3d = jp + npoints_per_plane * ipl

sol["ems"][ip3d] = data["ems"][jp,ispec] * ems_mod

sol["abs"][ip3d] = data["abs"][jp,ispec] * abs_mod

t2 = time.time()

print "plain python: %0.3f seconds" % ( t1 - t0 )

print "numpy: %0.3f seconds" % ( t2 - t1 )

附加說明:

我弄清楚如何快速做內部迴圈,但不是外迴圈:

# numpy vectorization

for ip in xrange(npoints_per_plane):

jp = naxis + ip

sol["ems"][jp:jp+npoints_per_plane*nplanes:npoints_per_plane] = data["ems"][jp,ispec] * ems_mod

sol["abs"][jp:jp+npoints_per_plane*nplanes:npoints_per_plane] = data["abs"][jp,ispec] * abs_mod

joe的解決方案顯示了如何一起做兩個,謝謝!

Python之迴圈優化

儘量減少迴圈內部不必要的計算 巢狀迴圈中,儘量減少內層迴圈的計算,盡可能向外提。區域性變數查詢較快,盡量使用區域性變數 import time start time.time for i in range 10000 lib1 for x in range 10000 1000 x 1000 end...

python效率怎麼樣 如何優化python的效率

優化python的效率的方法 1 優化演算法時間複雜度 2 減少冗餘資料 3 合理使用copy與deepcopy 4 使用dict或set查詢元素 5 合理使用生成器 generator 和yield。優化方法 1 優化演算法時間複雜度 演算法的時間複雜度對程式的執行效率影響最大,在python中可...

python雙重for迴圈優化方法。

用python做影象處理。有些特殊需求需要用雙重for迴圈遍歷影象來操作例如下面 def getbinarizationimg simg,targeth,targetw print simg.shape h,w,c simg.shape box np.zeros h,w dtype np.uint8...