简体   繁体   English

Python,如何优化这段代码

[英]Python, how to optimize this code

I tried to optimize the code below but I cannot figure out how to improve computation speed. 我试图优化下面的代码,但我无法弄清楚如何提高计算速度。 I tried Cthon but the performance is like in python. 我试过Cthon,但性能就像在python中。

Is it possible to improve the performance without rewrite everything in C/C++? 是否有可能在不重写C / C ++中的所有内容的情况下提高性能?

Thanks for any help 谢谢你的帮助

import numpy as np

heightSequence = 400
widthSequence = 400
nHeights = 80

DOF = np.zeros((heightSequence, widthSequence), dtype = np.float64)
contrast = np.float64(np.random.rand(heightSequence, widthSequence, nHeights))

initDOF = np.zeros([heightSequence, widthSequence], dtype = np.float64)
initContrast = np.zeros([heightSequence, widthSequence, nHeights], dtype = np.float64)
initHeight = np.float64(np.r_[0:nHeights:1.0])
initPixelContrast = np.array(([0 for ii in range(nHeights)]), dtype = np.float64)


# for each row
for row in range(heightSequence):
    # for each col
    for col in range(widthSequence):

        # initialize variables            
        height = initHeight # array ndim = 1
        c = initPixelContrast # array ndim = 1

        # for each height            
        for indexHeight in range(0, nHeights):
            # get contrast profile for current pixel
            tempC = contrast[:, :, indexHeight]
            c[indexHeight] = tempC[row, col]

        # save original contrast            
        # originalC = c
        # originalHeight = height                

        # remove profile before maximum and after minumum contrast
        idxMaxContrast = np.argmax(c)
        c = c[idxMaxContrast:]
        height = height[idxMaxContrast:]

        idxMinContrast = np.argmin(c) + 1
        c = c[0:idxMinContrast]
        height = height[0:idxMinContrast]              

        # remove some refraction
        if (len(c) <= 1) | (np.max(c) <= 0):
            DOF[row, col] = 0                  

        else:

            # linear fitting of profile contrast                                             
            P = np.polyfit(height, c, 1)
            m = P[0]
            q = P[1]

            # remove some refraction               
            if m >= 0:
                DOF[row, col] = 0

            else:
                DOF[row, col] = -q / m

    print 'row=%i/%i' %(row, heightSequence)

# set range of DOF
DOF[DOF < 0] = 0
DOF[DOF > nHeights] = 0

By looking at the code it seems that you can get rid of the two outer loops completely, converting the code to a vectorised form. 通过查看代码,您似乎可以完全摆脱两个外部循环,将代码转换为矢量化形式。 However, the np.polyfit call must then be replaced by some other expression, but the coefficients for a linear fit are easy to find, also in vectorised form. 但是, np.polyfit调用必须用其他表达式替换,但线性拟合的系数很容易找到,也是矢量化形式。 The last if-else can then be turned into a np.where call. 最后的if-else然后可以变成np.where调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM