Efficiency of Code Functionally Similar to if()
Posted by Eclipse1255@reddit | learnprogramming | View on Reddit | 4 comments
I want to know how efficient (or inefficient) if statements are to their alternatives and in what ways. I'm certain there are many alternatives I don't know---and I do want to hear about them---but in particular the following two ways.
To keep it simple, assume x is either 0 or 1 and thisFunction() does something. Before the two ways, the if statement I'll translate:
if x==1:
thisFunction()
#Code End
for i in range(x):
thisFunction()
#Code End
list=[doesNothing, thisFunction]
list[x]()
#Code End
Aside from if statement alternatives, what are ways I can compare the efficiency of two segments of code?
WellHung67@reddit
Functionally all three of those will be identical in performance. If this is a compiled language, we are talking exactly the same. By that I mean, the compiler will notice that effectively it’s the same thing and it will likely compile ultimately to a single assembly type instruction - likely a branch if not 0. It runs so fast, it’s already done. The exception is the “for” loop, which may have an extra instruction to decrement the value of “I” before doing a similar branch if not 0. And maybe that table lookup, it will load both function pointers into some register and doing a branch if not 0. Like I said, the same branch instruction is probably used and maybe one or two other instructions are the difference between all 3.
In general when learning programming always write things that are extremely readable and maintainable first, and don’t worry about optimizing unless the optimization is readable and also free or cheap to implement - using a simple sorting algorithm over bogosort for example. Do not worry about which type of if system t to use. Remember, code is read about 10x as much as it’s written so first optimize for readability.
When you optimize, generally first profile your code to see where it spends the most time executing, and the target those “hot spots”. More than likely, an if statement to a function like this is rarely run, so even if it ran instantaneously, the program execution time wouldn’t change much.
This actually is a good segue into what’s known as Amdahls law - if you respond to this comment I’ll explain how it’s relevant here
Suspicious_Coat3244@reddit
In fact, simple if is often preferred. It's transparent, easy and most importantly, already heavily optimized.
Iteration overhead makes the loop version less efficient, and function list trick adds lookup/dispatch overhead just to skip a simple conditional.
Most of time, readability takes precedence over the micro's optimization of this sort. Real bottlenecks are I/O, DB query, network, rendering etc., rather than the if statement.
rjcarr@reddit
Set timers and print the difference. If the function is simple, though, you’ll probably have to print nanos to tell a difference.
johnpeters42@reddit
It depends on lots of things. Test them both and find out.
Also, make sure it matters: if it's only gonna run (say) once a minute, and the difference is 0.1 vs 0.2 seconds, then who cares? If it's gonna run 10,000 times a minute, or if the speed is 5 seconds vs 10 seconds, then speed matters more.