如何使用 Numba 加速此功能?

How to accelerate this function using Numba?

我试图使用 Numba 优化此功能,但我无法做到。我认为这没有可以加速的代码部分。如果有人能帮我优化这个版本,我的程序就会变得非常快。请告知是否需要任何数据集或其他信息。当我对此应用直接@jit 时,它不起作用。

def c_a(x, y, z, counter, p_l):
    # start = time.time()
    if counter == 1:
        l = x
        m = y
        n = z


        path = "c_r.pdb"
        global r_a_t


        p = Bio.PDB.PDBParser() 
        structure = p.get_structure('mSN1', path)
        c_r = [a.get_coord() for a in structure.get_atoms()]   
        lengthnew = len(c_r)


        m_d = np.array([-45, -45, -45])


        a_s_r = np.zeros((128, 128, 128), np.complex)
        for i in range(0, lengthnew):
            x = int(math.floor((c_r[i][0] - m_d[0]) / 1.2))
            y = int(math.floor((c_r[i][1] - m_d[1]) / 1.2))
            z = int(math.floor((c_r[i][2] - m_d[2]) / 1.2))
            with open("Ei.txt", 'r') as ei_values:
                for row in ei_values:
                    s_v = row.split()
                    if s_v[0] == r_a_t[i] :
                        a_s_r[x, y, z] = np.complex(s_v[1])


        n_n = lambda x, y, z : [(x2, y2, z2) for x2 in range(x - 5, x + 6)
                                   for y2 in range(y - 5, y + 6)
                                   for z2 in range(z - 5, z + 6)
                                   if (-1 < x < X and
                                       -1 < y < Y and
                                       -1 < z < Z and
                                       (x != x2 or y != y2 or z != z2) and
                                       (0 <= x2 < X) and
                                       (0 <= y2 < Y) and
                                       (0 <= z2 < Z) and
                                       ((( abs(x - x2)) ** 2 + (abs(y - y2)) ** 2 + (abs(z - z2)) ** 2  ) <= 25))]  
        m = n_n(l, m, n)
        result = 0
        for i in range(0, len(m)):
            a = m[i][0]
            b = m[i][1]
            c = m[i][2]
            result = result + a_s_r[a][b][c]
        return result

    else:
        l = x
        m = y
        n = z
        path = p_l


        global l_a_t


        p = Bio.PDB.PDBParser() 
        structure = p.get_structure('mSN1', path)
        c_l = [a.get_coord() for a in structure.get_atoms()]   
        lengthnew = len(c_l)


        m_d = np.array([-45, -45, -45])


        a_s_l = np.zeros((128, 128, 128), np.complex)
        for i in range(0, lengthnew):
            x = int(math.floor((c_l[i][0] - m_d[0]) / 1.2))
            y = int(math.floor((c_l[i][1] - m_d[1]) / 1.2))
            z = int(math.floor((c_l[i][2] - m_d[2]) / 1.2))
            with open("E.txt", 'r') as e_v:
                for row in e_v:
                    s_v = row.split()
                    if s_v[0] == l_a_t[i] :
                        a_s_l[x, y, z] = np.complex(s_v[1])


        n_n = lambda x, y, z : [(x2, y2, z2) for x2 in range(x - 5, x + 6)
                                       for y2 in range(y - 5, y + 6)
                                       for z2 in range(z - 5, z + 6)
                                       if (-1 < x < X and
                                           -1 < y < Y and
                                           -1 < z < Z and
                                           (x != x2 or y != y2 or z != z2) and
                                           (0 <= x2 < X) and
                                           (0 <= y2 < Y) and
                                           (0 <= z2 < Z) and
                                           (((abs(x - x2)) ** 2 + (abs(y - y2)) ** 2 + (abs(z - z2)) ** 2  ) <= 25))]  
        m = n_n(l, m, n)
        result = 0
        for i in range(0, len(m)):
            a = m[i][0]
            b = m[i][1]
            c = m[i][2]
            result = result + a_s_l[a][b][c]
        # print "c_a : ", time.time() - start    
        return result

已解决。

将所有 文件读取步骤 带出函数外,因为它们被执行了很多次。它带来了 70 倍 的提升。

刚刚在函数中留下了 lambda 函数,因为它们依赖于 xy & z.