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

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

Indently

1 год назад

13,218 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@legionnex
@legionnex - 12.08.2023 23:14

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

Ответить
@oida10000
@oida10000 - 21.06.2023 20:32

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

Ответить
@Pawlo370
@Pawlo370 - 30.05.2023 01:19

... is the same thing like pass?

Ответить
@Syph0nn
@Syph0nn - 27.05.2023 02:41

what editor is this?

Ответить
@gabi_kun3455
@gabi_kun3455 - 26.05.2023 06:28

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

Ответить
@viktoreidrien7110
@viktoreidrien7110 - 25.05.2023 23:40

Excellent video man, thanks !!

Ответить
@Randych
@Randych - 25.05.2023 22:19

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

Ответить
@migueldavidfernandezmoreir6232
@migueldavidfernandezmoreir6232 - 25.05.2023 16:42

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?

Ответить
@legitjimmyjaylight8409
@legitjimmyjaylight8409 - 25.05.2023 15:59

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.

Ответить
@Mulakulu
@Mulakulu - 25.05.2023 13:58

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.

Ответить
@brianhourigan
@brianhourigan - 25.05.2023 13:37

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.

Ответить
@noomade
@noomade - 25.05.2023 13:19

10 million...

Ответить
@Alister222222
@Alister222222 - 25.05.2023 10:25

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.

Ответить
@LiquidMasti
@LiquidMasti - 25.05.2023 09:11

Which method is best? Pretty vague answer in video

Ответить
@AWriterWandering
@AWriterWandering - 25.05.2023 08:55

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.

Ответить
@Andrumen01
@Andrumen01 - 25.05.2023 08:45

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.

Ответить
@samvelsafaryan4698
@samvelsafaryan4698 - 25.05.2023 08:13

Match case vs if else !!!!! Battle

Ответить
@rishiraj2548
@rishiraj2548 - 25.05.2023 08:03

🙏👍

Ответить
@alphanow4199
@alphanow4199 - 25.05.2023 07:57

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

Ответить
@billyyank2198
@billyyank2198 - 25.05.2023 07:29

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')

Ответить
@boopalanpichandi8178
@boopalanpichandi8178 - 25.05.2023 07:01

Ответить