If-else statement vs ternary operator (Performance) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

If-else statement vs ternary operator (Performance)

Is there a significant difference in terms of performance (running time, running space, etc) between if-else statements and ternary operators? # if-else statement if average >= 75: print(“Pass”) else: print(“Fail”) # ternary operator / inline statement print(“Pass” if average >= 75 else “Fail”)

18th Jan 2020, 9:29 AM
Zerokles
Zerokles - avatar
2 Answers
+ 2
I made a speed test with 2 equivalent statements, but they seem to be very close, but if else statement seems to be a little faster: https://code.sololearn.com/c0Q4j7fv6Rwb/?ref=app
18th Jan 2020, 9:40 AM
Seb TheS
Seb TheS - avatar
+ 1
I dont think there is any difference. The second is basically short of the first
18th Jan 2020, 9:41 AM
Sajid
Sajid - avatar