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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to be looking at whether it's fast that you have a lot of if statements or whether it's faster to have if a live and else and there are use cases for both so let's jump into it immediately by writing the test cases so first of all I'm going to import from time it the time it function now for the consecutive if statements we're going to create a test called test if which will take some text of type string and here we'll type in if text is equal to a will do nothing if text is equal to B we'll still do nothing and finally if text is equal to C will still do nothing so it's a very simple test and we can also create the exact same test using the if else so next we have Dev test if else which also takes text of type string and I'm going to copy and paste in the test so here we're checking if text is equal to a do nothing if text is equal to B do nothing alif text is equal to C do nothing else just continue doing nothing and to simulate this better we'll also add the doing nothing at the bottom of the test if so those are the basic tests next let's check that name is equal to Main and let's create a letter of type string which is initially going to be set to a then let's get the times using time it so if chain time of type float is going to equal time it and here we need to pass in first the statement so test if with the letter and the globals are going to equal the globals then we can just duplicate that and say if else chain time until we have to type in if else and we're going to insert the letter again then we can print if if for the consecutive ifs and we want to round the result so we're going to round the number of the if chain time to three decimal places to keep things simple we will do the same thing with if else so we'll type in if else and here we'll type in if else chain time rounded to three decimal places now first it's important to see if everything works so we're just going to click on run and so far everything works we have the if if which takes a bit longer than the if else and of course there's a lot to explain here because it's not just straightforward saying that if if is slower than if L if else there are some factors that come into account so in this scenario we're searching for a which is the first element in the if else and in the if if now obviously you probably are thinking okay it makes sense because if text evaluates to true so this code will be executed but it does not stop the other ones from being executed so that does take some additional processing time so in this scenario searching for a is a huge mistake but in test if else it searches for a and it understands that okay a is equal to text so it stops here and it doesn't execute anything else making it much faster in this situation but if you search for an element such as D which is in none of these cases and in none of these cases it's going to go all the way to the bottom executing each one of these until it finds out that there's nothing there so it executes the else and the same thing goes with test if if we're looking for D it's not going to find it in the first three if statements so it's going to execute the remainder of the code and if we actually run this or go to the bottom and run this you'll see that the time is going to be nearly identical so sometimes the tests are going to be nearly identical sometimes they're not in this case if if is still a bit slower according to these tests so right now with the amount of tests we run we're always getting that if else is slightly faster and this won't always be the case because there are a lot of factors that come into play such as the computer resources of course so something else we can do is is increase the number of tests so I'm going to increase it to a million from what I believe was a hundred thousand earlier and by increasing the amount of tests you get something a bit more stable so now if we're running it a million times instead of a hundred thousand times you'll see that when we run the program we're going to get numbers that are much more closely related because now it's more able to stabilize the results so like this we're still getting something that's more like a micro optimization but now it's time to cover the next scenario and that is that if you're using chained if statements there's a big chance you're probably also going to be using the god Clause statement so using this without the god Clause statement might end up being quite inefficient what you really would want to do is return some sort of result early so you don't have to waste your time with the rest of them so as soon as you know something is something return out of the function so you don't have to waste your time with the other if statements otherwise return false we're going to do the same thing for the test if else so return true return true and return false and just like that now if we go to let's say B and we run the script we're going to get times that are nearly identical so if we run it and run it again you'll see that the times are nearly identical but let's change this letter to D so let's go for the worst case scenario which is the else block and that's the same thing with the consecutive if statements that is the worst case scenario if we run it with d we're going to get something I mean here we had if if that was even slightly faster but again these are not going to be consistent results because computers have lots of things going on so sometimes when you run it one is going to be faster other times the other is going to be faster depending on the resources of your computer and the current implementation of python so at the end of the day this is a huge micro optimization depending on how you use it if you are returning early and using the guard claw statement it shouldn't really make that much of a difference compared to just using L if there is a reason to use this it's not so common in Python from what I've seen you would see it used much more often in languages such as Swift but otherwise if you are just creating if statements one after the other that execute code do keep in mind that this will not stop the next if statement from being executed so that will result in a huge time loss because it has to do the check each time you're going through it while with the if else statement it does the check if it is true it stops here and that is done even if you have code that does not return anything if this is true it's not going to check any of the other code which means you're saving resources but anyways I hope you found this video interesting do let me know in the comment section down below if you have anything to add regarding if else versus if if because even if python wasn't really made for optimizations I still find this kind of stuff very interesting so please let me know in the comment section down below if you know anything else regarding this but anyways with all that being said as always thanks for watching and I'll see you in the next video
Info
Channel: Indently
Views: 13,023
Rating: undefined out of 5
Keywords: pyton, pyhton, pythn
Id: O6yHJl0ThKY
Channel Id: undefined
Length: 7min 17sec (437 seconds)
Published: Thu May 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.