In computer architecture class, I learned that when the "if" statement is executed in assembly language, it involves the use of branch prediction strategies. Furthermore, it was emphasized that the operation of branch prediction incurs a significant cost.
To explore this concept further, I created the following example. I initially anticipated that the presence of the "if" statement would result in longer execution times. However, to my surprise, the code with the "if" statement actually executed faster, and the difference in execution time was nearly two-fold (1.8 seconds for the code with "if" vs. 2.5 seconds for the code without "if").
I have contemplated this matter but have been unable to pinpoint the reason for the nearly two-fold difference in execution time. I would greatly appreciate any advice or insights on aspects I may not have considered.
import time
def without_if(x):
arr = ["Zero","One", "Two", "Three", "Four", "Five"]
return arr[x]
num_iterations = 10000000
start_time = time.time()
for _ in range(num_iterations):
result = without_if(5)
end_time = time.time()
print(f"Result: {result}")
print(f"Execution time without if: {end_time - start_time}")
import time
def with_if(x):
if x == 1:
return "One"
elif x == 2:
return "Two"
elif x == 3:
return "Three"
elif x == 4:
return "Four"
elif x == 5:
return "Five"
else:
return "Unknown"
num_iterations = 10000000
start_time = time.time()
for _ in range(num_iterations):
result = with_if(5)
end_time = time.time()
print(f"Result: {result}")
print(f"Execution time with if: {end_time - start_time}")
I executed each of these codes 10 times, and in all cases, the time difference consistently remained at approximately two-fold.
I suspected that the issue might be related to the programming language, so I converted the same code to Java and executed it, but I still observed the consistent two-fold difference in execution time.
As mentioned in the comments you should use the
timeitfunction to get execution time for a specific number of repetitions.Also note that you are not really comparing the same thing! You tried comparing creating a list and accessing an element by its index to 6 if statements and retruning a string. Instead create the
listof elements to acess bywithout_ifoutside of the function, because this operation is slowingwithout_ifdown:Output:
Maby consider this approach to even more equalize the two functions to focus on the effect of the
ifstatement:wich returns for me:
The answer to this question might interesst you. Also according to this paper: