Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 使用多个嵌套for循环加速python脚本_Performance_Python 3.x_For Loop_Nested - Fatal编程技术网

Performance 使用多个嵌套for循环加速python脚本

Performance 使用多个嵌套for循环加速python脚本,performance,python-3.x,for-loop,nested,Performance,Python 3.x,For Loop,Nested,我有一个python脚本,其中大多数操作都是在嵌套for循环中完成的。 由于我打算将此脚本用于大型数据集,这将非常耗时。对于一个文件,这需要大约10秒。当使用2000个文件时,将需要5个多小时。 有什么方法可以加速这个代码吗 对应代码: dir_path = 'path to directory where files are stored' OverScan = 0 sizeY = 480 cols = 96 reg = OverScan + 10 sizeX = cols * reg YT

我有一个python脚本,其中大多数操作都是在嵌套for循环中完成的。 由于我打算将此脚本用于大型数据集,这将非常耗时。对于一个文件,这需要大约10秒。当使用2000个文件时,将需要5个多小时。 有什么方法可以加速这个代码吗

对应代码:

dir_path = 'path to directory where files are stored'
OverScan = 0
sizeY = 480

cols = 96
reg = OverScan + 10
sizeX = cols * reg
YTOT = 2 * sizeY

fmt = '>'+str(192*(1+sizeY)*reg)+'H'
length = struct.calcsize(fmt)
size = 'size of the directory'

NmbImg = size//length
Images = np.zeros((sizeX,YTOT,NmbImg))
gImages = np.zeros((sizeX,YTOT,NmbImg))
bits13 = np.zeros((sizeX,YTOT,NmbImg))

for filename in glob.glob(os.path.join(dir_path, '*.bin')):

    with open(filename, "rb") as pathSig:
        s = pathSig.read(2*192*(1+sizeY)*reg)
        data = np.array(struct.unpack_from(fmt,s))
        data2 = data & 0b1111111111111
        data3 = data >> 14
        bit13 = data >> 13 & 0b1  
        ktr = 7*192
        for y in range(0, sizeY):
            for x in range(0, OverScan+10):
                for i in range(0, 192):
                    xdex = ChanMap2[i] + (9-x)
                    pVal = data2[ktr]
                    gVal = data3[ktr]
                    bVal = bit13[ktr]
                    if TopBot[i] == 0:
                        Images[xdex, y,counter] = pVal
                        gImages[xdex, y,counter] = gVal
                        bits13[xdex, y,counter] = bVal
                    else:
                        Images[sizeX-xdex-1, YTOT-y-1,counter] = pVal
                        gImages[sizeX-xdex-1, YTOT-y-1,counter] = gVal
                        bits13[sizeX-xdex-1, YTOT-y-1,counter] = bVal
                    ktr += 1  
注意:ChanMap2是映射函数定义的映射数组


非常感谢您的帮助。

查看
numpy.vectorize()
和/或子流程。似乎并行处理多个文件会有巨大的优势。谢谢你的建议。