我如何加速python嵌套循环?

时间:2011-10-12 17:28:49

标签: python

我试图通过计算身体两侧的影响来计算埋藏物体的重力效应,然后总结贡献以在一个站点获得一次测量,重复多个站点。代码如下(正文是一个正方形,代码围绕它顺时针计算,这就是它从-x返回到-x坐标的原因)

grav = []
x=si.arange(-30.0,30.0,0.5)

#-9.79742526     9.78716693    22.32153704    27.07382349  2138.27146193
xcorn = (-9.79742526,9.78716693 ,9.78716693 ,-9.79742526,-9.79742526)
zcorn = (22.32153704,22.32153704,27.07382349,27.07382349,22.32153704)
gamma = (6.672*(10**-11))#'N m^2 / Kg^2'
rho = 2138.27146193#'Kg / m^3'
grav = []
iter_time=[]
def procedure():
    for i in si.arange(len(x)):# cycles position
        t0=time.clock()
        sum_lines = 0.0

        for n in si.arange(len(xcorn)-1):#cycles corners
            x1 = xcorn[n]-x[i]
            x2 = xcorn[n+1]-x[i]
            z1 = zcorn[n]-0.0  #just depth to corner since all observations are on the surface.
            z2 = zcorn[n+1]-0.0
            r1 = ((z1**2) + (x1**2))**0.5
            r2 = ((z2**2) + (x2**2))**0.5 
            O1 = si.arctan2(z1,x1)
            O2 = si.arctan2(z2,x2)
            denom = z2-z1
            if denom == 0.0:
                denom = 1.0e-6

            alpha = (x2-x1)/denom

            beta = ((x1*z2)-(x2*z1))/denom
            factor = (beta/(1.0+(alpha**2)))
            term1 = si.log(r2/r1)#log base 10
            term2 = alpha*(O2-O1)
            sum_lines = sum_lines + (factor*(term1-term2))
        sum_lines = sum_lines*2*gamma*rho
        grav.append(sum_lines)
        t1 = time.clock()
        dt = t1-t0
        iter_time.append(dt)

任何有关加快此循环的帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

你的xcorn和zcorn值重复,所以请考虑缓存一些计算的结果。

查看timeit和profile模块,以获取有关计算时间最多的信息。

答案 1 :(得分:1)

在Python循环中访问numpy数组的各个元素是非常低效的。例如,这个Python循环:

for i in xrange(0, len(a), 2):
    a[i] = i

会慢得多:

a[::2] = np.arange(0, len(a), 2)

您可以使用更好的算法(更少的时间复杂度)或在numpy数组上使用向量运算,如上例所示。但更快的方法可能只是使用Cython编译代码:

#cython: boundscheck=False, wraparound=False
#procedure_module.pyx
import numpy as np
cimport numpy as np

ctypedef np.float64_t dtype_t

def procedure(np.ndarray[dtype_t,ndim=1] x, 
              np.ndarray[dtype_t,ndim=1] xcorn):
    cdef:
        Py_ssize_t i, j
        dtype_t x1, x2, z1, z2, r1, r2, O1, O2 
        np.ndarray[dtype_t,ndim=1] grav = np.empty_like(x)
    for i in range(x.shape[0]):
        for j in range(xcorn.shape[0]-1):
            x1 = xcorn[j]-x[i]
            x2 = xcorn[j+1]-x[i]
            ...
        grav[i] = ...
    return grav

没有必要定义所有类型,但如果你需要比Python更快的速度,你应该至少定义数组和循环索引的类型。

您可以使用cProfile(Cython支持),而不是手动调用time.clock()

致电procedure()

#!/usr/bin/env python
import pyximport; pyximport.install() # pip install cython
import numpy as np

from procedure_module import procedure

x = np.arange(-30.0,30.0,0.5)
xcorn = np.array((-9.79742526,9.78716693 ,9.78716693 ,-9.79742526,-9.79742526))
grav = procedure(x, xcorn)