0

Is there an efficient and faster way in dealing with Multiple Combinational Conditions such as this:

if(condition1 and condition2):
    statements
elif((condition1 or condition2) and not condition3):
    statements
elif((condition3 and condition2) and not condition1):
    statements
elif(condition1 or condition2):
    statements
else:
    statements

my colleagues said that I should implement a switch case (dict), but I'm not sure how to implement the multiple combined conditions.

Thank You in Advance.

  • First thing I found when I searched "switch case dict". https://stackoverflow.com/questions/21962763/using-a-dictionary-as-a-switch-statement-in-python – ctrl-alt-delor Jun 08 '22 at 12:22
  • As for big-O. The big O of what you have is O(n), where n is the number of conditions. If they are small, then it probably does not matter. – ctrl-alt-delor Jun 08 '22 at 12:24
  • What is your concern with memory management? – ctrl-alt-delor Jun 08 '22 at 12:26
  • Correct me if I'm wrong, I worry that when checking for multiple conditions it might take a huge chunk of memory, – Remian-Feral Jun 09 '22 at 10:50
  • I'm trying to optimize my neural network's actions, its actions should depend on certain appropriate conditions. – Remian-Feral Jun 09 '22 at 11:00
  • Why do you think that (it will take lots of memory)? It only needs to express what is in the code. If you can write it is a few bytes then it is a few bytes. However all conditional code can cause a pipeline stall. I am not sure that this should be a concern in python. If you are processing a list of data, then use filter, map, and maybe reduce. It should run much faster. Then there are libraries such that may do it even faster (they are written in C or Fortran, so are fast). – ctrl-alt-delor Jun 10 '22 at 21:06
  • The best solution will depend on whether your statement is a procedure (does an action) or a function (calculates and returns a value). It may also depend on other things. – ctrl-alt-delor Jun 10 '22 at 21:08
  • Thank You for your input! I'll try to implement your solutions. The statement does an action (it doesn't really calculate data). Any recommendations or forums for filtering, and mapping data? – Remian-Feral Jun 11 '22 at 10:27
  • I'm sorry if I sounded uninformed you mentioned C or Fortran, will it run within python or ? – Remian-Feral Jun 11 '22 at 10:29
  • Yes some python libraries are written in other languages. You don't have to worry: you can just ignore the implementation. Just install and use. – ctrl-alt-delor Jun 11 '22 at 15:33

0 Answers0