mardi 7 novembre 2017

Do if statements take significant amounts of time in python?

I am creating a graphing program which has to iterate values through a calculation 10000-1000000 times, and then append part of that output to a list. In order to change which list it is appended to, there are ~3 if statements inside that loop. While it would logically be faster to use the if statement first, is there a significant amount of time saved?

As an example:

output = []
append_to = "pol"
for i in range(10000):
    if append_to == "pol":
        output.append(np.cos(i))
    else:
        output.append(np.sin(i))

Would this be significantly slower than:

output = []
append_to = "pol"
if append_to == "pol":
    for i in range(10000):
        output.append(np.cos(i))
else:
    for i in range(10000):
        output.append(np.sin(i))

Aucun commentaire:

Enregistrer un commentaire