1

I have many if statements and I want to find the most optimized sequence for them.

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 the second one 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
Ralf
  • 16,086
  • 4
  • 44
  • 68
Hyrial
  • 1,728
  • 3
  • 15
  • 27
  • 4
    @Cid, there is no switch case in Python. You can use a mapping dictionary, for example. – Austin Feb 14 '19 at 08:46
  • Maybe you are looking for a profiling tool, e.g. from the [standard library](https://docs.python.org/3.7/library/profile.html). – mportes Feb 14 '19 at 09:26

3 Answers3

2

To make an informed design decision, you need to define your goal:

  1. If the function should return as fast as possible, then the fastest case should probably go first.
  2. But if you want the function to return fast on average with all input data, then you probably need to know which case is more likely for your input data; the case where you expect the most input data to fall into, that one should probably be first. That way, the average run time of the function could be reduced.

But to make the best decision you need to consider a lot of factors:

  • the type of your data (sets, lists, strings, ...)
  • the size of the input and how many times the function will be invoked
  • the operations that are done in each case (is it a search, some heavy computation, a simple arithmetic operation, ...)
  • investigate the possibility to include as many shortcircuit conditions in your cases to have them fail early

If you want more help, then you should are more specific information to your question.

Ralf
  • 16,086
  • 4
  • 44
  • 68
1

What is your goal? Is it the reduction of the average time, the worst-case time or some other measure?

Minimum average time: If you have statistical information about the necessary time for each case, of its relative probability and the time for a non-satisfied if condition, you can calculate the expectation value for each sequence of if-clauses and choose the best one.

(If applicable, avoiding the sequence of if-clauses - as Austin and Shadab Ahmed have already proposed - would be a good idea.)

0

Try below code if you want something like switch

def f(x):
return {
    'a': 1,
    'b': 2,
}[x]

If you want to run all if conditions then you can try to run the functions in parallel and the worst time complexity will be the max of all the functions. example for running functions in parallel Python: How can I run python functions in parallel?

Shadab Ahmed
  • 556
  • 9
  • 20
  • 3
    This answer does not have much information. Could you add a bit more reasoning? What are you trying to say with this code? How does this help the asker? – Ralf Feb 14 '19 at 10:24
  • this code is quick implementation of switch statement and will only help if we know which specific case we need to run but yes it will not be helpful if all conditional cases need to be checked before returning bool. in that cases all the conditional cases be made function and run in parallel. On how to run functions parallely check here https://stackoverflow.com/questions/7207309/python-how-can-i-run-python-functions-in-parallel – Shadab Ahmed Feb 14 '19 at 11:49