python优化循环_Python:优化循环

博主正在寻求优化包含两个嵌套循环的Python代码,他们尝试使用numpy的range和迭代器,但发现这比标准的Python实现更慢。问题在于numpy的使用方式。已提出一个解决方案,通过numpy的向量化操作改进了内部循环,但外循环仍有待优化。社区成员提供了建议,展示了如何同时优化内外循环,显著提高了代码效率。
摘要由CSDN通过智能技术生成

我希望优化一些由两个嵌套循环组成的

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

# this is where the data is being mapped to

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 )

编辑:将“jp = naxis ip”放在第一个for循环中

附加说明:

我弄清楚如何快速做内部循环,但不是外循环:

# 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的解决方案显示了如何一起做两个,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值