I am writing a script that parses user input, reads a file, does some work, and then sends an html POST request.
There is one section of the code that requires me to do a different action based on the interaction of three variables, but some of the cases are handled with 'do nothing' i.e. pass. Initially, I included all of the pass conditional paths to help with maintainability for the other employees that are not as familiar with this script/Python.
So with this information, my question is:
Will there be any kind of performance speedup if I remove these pass paths? These statements are not in a module that will be imported anywhere else, it will be run from the command line.
The original code is something like the following:
if var1 is not None:
if var3 == var1 or var3 == 'SPECIFIC VALUE':
xml.update_element()
xml.remove_element()
elif var2 == var3:
pass
else:
xml.update_element()
else:
if var3 == 'SPECIFIC VALUE':
pass
elif var2 != var3:
xml.update_element()
xml.create_element()
And I changed it to:
if var1 is not None:
if var3 == var1 or var3 == 'SPECIFIC VALUE':
xml.update_element()
xml.remove_element()
elif var2 != var3:
xml.update_element()
else:
if var3 != 'SPECIFIC VALUE' and var2 != var3:
xml.update_element()
xml.create_element()
My advice would be to go for readability: if your code will be read by many people, make it as simple and as comprehensible as you can. Anything else would be premature optimization.
You can actually do tests with the timeit module to determine whether the speed gains are significant or not. If they aren't, forget them and leave your program readable.
In many cases if conditions actually speed up the program (because we are skipping over blocks of code). But that is not the case as nothing is done inside the blocks.
The only time large numbers of if statements will slow down the code execution is if the condition you're checking requires processing. An example would be something like (just to make a point, it does nothing):
while True:
if sum(alist) == 0:
pass
Every iteration through the loop checks if sum(alist) == 0. This means that every iteration, the interpreter has to go and sum the elements of the array since it may have changed.
Performance wise you if blocks with pass should not make a significant difference, but it all depends on the conditions you are checking. The order of those will have consequences depending on the program itself.
Nevertheless, having too many if statements can reduce code readability. In many cases, if you can remove a conditional without it affecting the behavior of the code, then you didn't need it to begin with.
For the modifications shown, it does not make a difference on the (outer) if block - should be practically the same since in both cases there are two conditions to be checked.
if var1 is not None:
## CONDITION 1
if var3 == var1 or var3 == 'SPECIFIC VALUE':
xml.update_element()
xml.remove_element()
## CONDITION 2
elif var2 == var3:
pass
## NO CONDITION TO CHECK
else:
xml.update_element()
In the modified version:
if var1 is not None:
## CONDITION 1
if var3 == var1 or var3 == 'SPECIFIC VALUE':
xml.update_element()
xml.remove_element()
## CONDITION 2
elif var2 != var3:
xml.update_element()
But on the (outer) else block you removed one of the conditions so in some cases you will save up some time (and is more readable).
else:
## CONDITION 1
if var3 == 'SPECIFIC VALUE':
pass
## CONDITION 2
elif var2 != var3:
xml.update_element()
xml.create_element()
While in the modified version:
else:
## ONLY 1 CONDITION
if var3 != 'SPECIFIC VALUE' and var2 != var3:
xml.update_element()
xml.create_element()
If it is significant or not only your timed tests can tell. Even though the condition is a bit more complex, if var3 == 'SPECIFIC VALUE' it immediately return false and the second condition is not checked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With