有没有办法加快循环速度?

我需要以更有效的方式编写此块:

    for aci in teta_degree:                     
        for t_degeri in t:
            for x_degeri in x_values:
                resulted_y_values = np.tan(aci) * x_degeri + t_degeri / np.cos(aci)
                result.append([aci,t_degeri,x_degeri,resulted_y_values])

通过使用itertools,我可以替换for循环,但是找不到将result_y_values放入结果中相关位置的方法:

result_2 =  [i for i in itertools.product(teta_degree,t,x_values)]
zj005386 回答:有没有办法加快循环速度?

感谢@PaulRooney,即使他(也是我)也没想到这一点,他的解决方案(即将np.tannp.cos操作带到了外部循环中也带来了出色的性能:

yeni_süre = time.time()
for aci in teta_degree:  
        tan = np.tan(aci)
        cos = np.cos(aci)
        for t_degeri in t:
            for x_degeri in x_values:
                resulted_y_values = tan * x_degeri + t_degeri / cos
                result.append([aci,t_degeri,x_degeri,resulted_y_values])
print('New: ',time.time()-yeni_süre,'sec')

1.061 sec

而旧的:

8.601 sec

本文链接:https://www.f2er.com/3077633.html

大家都在问