如何加快python中的循环速度?

时间:2019-06-22 06:53:08

标签: python-3.x performance for-loop time

我有这段代码,由五个嵌套循环组成,每个循环有100次迭代,但5小时后仍未完成。我是python的新手,所以我只写了最简单的代码。 谁能告诉我如何将python中的运行时间缩短到至少少于10秒?

def func(c1, a2, b2, c2, a3):
    val = (((c1+c2-a2+10-a3)/2) - ((3/2)*((((c1-a2)*(c1-a2))/(c1+b2-a2)) + (((c2-a3)*(c2-a3))/(c2+10-b2-a3)))))
    print("Value of function: {}".format(val))
    return val

for c1 in np.arange(0,10,0.1):
    print("in c1 loop")
    for a2 in np.arange(0,10,0.1):
        print("in a2 loop")
        for b2 in np.arange(0,10,0.1):
            print("in b2 loop")
            for c2 in np.arange(0,10,0.1):
                print("in c2 loop")
                for a3 in np.arange(0,10,0.1):
                    print("in a3 loop")
                    iter = iter + 1
                    print("Iteration: {}".format(iter))
                    if (a2<c1 and a3<c2 and b2<c2 and a2<b2 and a3>c1):
                        print("in condition")
                        val = func(c1, a2, b2, c2, a3)
                        if val == 0:
                            count = count + 1
                            print("Success")
                            induV = [c1, a2, b2, c2, a3, val]
                            valIsZero.append(induV)
print(count)
print(valIsZero)

0 个答案:

没有答案