'if VS. elif else' In Python, Which Is FASTER?

preview_player
Показать описание
Today we're going to be looking at the performance difference between "consecutive ifs" vs "if elif else" in Python. Which one is faster?

▶ Become job-ready with Python:

▶ Follow me on Instagram:
Рекомендации по теме
Комментарии
Автор

There are also conditions where multiple if statements would actually be true, yet you only want to run the first true statement. A classic example is printing a letter grade, where you can say…
if grade >= 90:
print(“A”)
elif grade >= 80:
print(“B”)
elif grade >= 70:
print(“C”)
elif grade >= 60:
print(“D”)
else:
print(“F”)
If you had a grade >=90, then by definition you would also have a grade >= 80, etc etc. But this won’t matter with an elif because the first condition was already true, and so the rest is skipped.

AWriterWandering
Автор

I believe that the simplest way to think about it is in terms of the old GOTO instruction...that is the underlying code at a simpler level. With the "if-if" case the GOTOs will always point to the end of the current if statement, whereas the "if-elif-else" will always point to the last condition, regardless of which condition it enters.

In terms of "Big O" notation, "if-if" will always be O(N), however, the "if-elif-else" will be O(1) at best, O(N) at worst. Simple analysis, but worth noting for any programming language.

Andrumen
Автор

By logic alone if-elif should be faster.

Lined ifs will run the check on each if, meanwhile if-elifs will do the same, until it gets to a positive case. I don't know the implementation under the hood, but it likely makes a goto to the end of the chain or it automatically checks False on each elif and else.

Match-case on the other hand, I don't know if it's like that in python, but often it's basically a dictionary, so there's the overhead of creating the hash map, but the operation itself is faster.

CarlosGonzalez-mpre
Автор

I do like this kind of video, comparing the different ways to do the same thing in Python. If one way is 10% faster, then that's something I want to know.

Alister
Автор

next: compare it to

def http_error(status):
match status:
case 400:
return "Bad request"
case 404:
return "Not found"
case 418:
return "I'm a teapot"
case _:
return "Something's wrong with the internet"


in Python 3.10

BennoKushnir
Автор

Which method is best? Pretty vague answer in video

LiquidMasti
Автор

Great!, A question I never asked myself. Great video!

legionnex
Автор

They dont do the same thing. If the condition changes to meet the next condition, elif will make sure that it doesnt do it, and else is everything not matched by the ifs and elifs.

legitjimmyjaylight
Автор

Great video bro, always watch him, and i have a doub, why you declare de type of the variables? is because is good practice, avoid some dinamic typing problems, or maybe is because more faster?

migueldavidfernandezmoreir
Автор

def what_should_I_do():
if timeit.if_if < timeit.if_else:
return if_else
else:
return if_if

print('I am such a nerd')

billyyank
Автор

10**7 are ten millions, not 1 that would be 10**6.

oida
Автор

i think the 2 kastd ones are producing the exact same assembly

alphanow
Автор

The first test isn't really identical, since with the "if if", the last ... Runs no matter what so it is functionally different. It's not like the "if else" where it only runs if all else fails.

I guess that was a part of the video too though, showing that these are fundamentally different. I just think a head to head test should use functionally equivalent functions.

Mulakulu
Автор

elif conditions does not exists, elif is really an further simplified and readable version of an if inside the else of the condition before it,
so the question is reduced to if vs if inside an else, and that does not make sense since an if can't replace an if inside an else since the if inside the else is connected with the else, while the if doesn't

gabi_kun
Автор

I have a simple rule:

Use If Else.

Using if and if not in chains makes it hard to re-read your own code weeks, months or years later.

If you are really worried about speed, then you probably shouldn't be using python in the first place.

StrayCatInTheStreets
Автор

Why do you need that crap?
Python has match since 3.11 or something.

Randych