jeudi 14 février 2019

How do I find the most optimized sequence of if statements?

Each case has a different time complexity and gets triggered a different amount of times. For example, case1 might be Θ(n^2) vs. case3 of Θ(log(n)) but stops the function earlier so it could be better to place it first.

How would I go about finding the most efficient way to order the if statements?

def function1():   
    if case1:
        return False
    if case2:
        return False
    if case3:
        return False
    # caseN...

    return True

Aucun commentaire:

Enregistrer un commentaire