Why People Use Python Even If It’s Slow

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so in this video we're going to look at why python is so popular even though it is quite slow and why speed isn't everything when choosing a programming language and that's exactly the message that i want to get across in this video that speed really isn't everything if speed was everything that everyone would be writing code in c or c plus or assembly in the case of python there are some really good reasons to give up a bit of speed because what you get in return is a lot of flexibility a very loose syntax that makes it more pleasant to work with and it's much easier to get comfortable with compared to compile languages like c and there are lots of different ways to actually measure popularity of different languages but in this video we're going to use the tob index i think that's how you say it and that's basically based on the popularity of the different languages which is calculated by finding out the number of search hits that it gets on different search engines if we look at the tob index for september 2021 we see that python is in second place at 11.86 percent right after c at 12.57 and it's overtaken java in the past year furthermore we see that python has increased massively in popularity with 2.7 more than any other language and in the meantime c has dropped in popularity with 4.41 and one of the reasons why python is doing so well right now is because of the increase in data mining and machine learning projects as an example the global machine learning market was valued at 1.58 billion dollars in 2017 and is expected to reach 20.8 billion dollars by 2024. this highlights another strong aspect of python that just because it's a beginner language doesn't mean that it's only for beginners it actually has plenty of professional use cases so to explore a little bit more about why python is so slow we're going to look at the programming language hierarchy of different languages in terms of speed and then we're also going to look at under the hood of how python actually works before diving into the details we can consider a simple example in python we're going to try to create a list of numbers from 0 to 10 million and then we're going to try to sum them if we do this the python way with the list and the built in sum function we get a runtime of 0.788 seconds which is pretty fast but what if we use c if we want to do the same thing in c we find that the same operation is much faster and we can run the script in just 0.029 seconds which is around 27 times faster than our python program for this exact same program so now what happened why was python so slow in this example the short answer to this is that python is an interpreted language which means that it doesn't run natively on your machine it actually runs virtually on your machine you have to actually create a virtual environment to run your python code if you've ever programmed in python using an ide then you will usually specify a virtual environment for your project before you do anything and this is the root of the slowness of python in contrast to this c is a compiled language and this means that to run the program you need to compile your code into machine language that runs directly on the cpu also a quick note in the next part of this video i've hidden two amazon gift cards one for fifty dollars and one for twenty five dollars so keep your eyes peeled and you might just find a twenty five or fifty dollar coupon code to amazon let me know if you thought this was a fun idea or not i i don't know if it was but anyway back to the video okay so in general we can sort of create a hierarchy of programming languages in terms of their proximity to the hardware that the code actually runs on starting from the bottom at the hardware level we use the cpu here the machine language code is chopped into micro code that is then executed by the processor machine language is basically just a lot of binary code ones and zeros and therefore it's not something that's easily readable by humans but it is the language of the cpu if we go up the hierarchy we find assembly and this is a language that is a more human friendly or human readable version of machine code and the structure of this code starts to resemble what we would see in our high level languages so going one step further we find these high level languages like c and c plus plus and these languages now are a lot more human friendly and a lot easier to work with but there are still some things that we need to consider like specifying the correct data types for instance and in many cases we also need to manage memory manually if we go up another step we find object-oriented languages like python and visual languages like javascript two very popular and very powerful languages but they're also rather abstract or many steps away from the machine language code that the cpu understands the reason why languages like c are so fast is because when we compile them we compile them straight into that lower level code that our machine or hardware can read this is however only possible because of the fact that these c like languages are very strict in terms of syntax which means that if you don't define your variables as integers strings or floats then your code just will not run or it might run but it will throw a lot of errors in your face if you want to dive a little bit deeper into this and learn more about how all this works then i really suggest going to brilliant.org and checking out some of their computer science courses where you can basically learn in depth how some of these things work and brilliant is very kindly sponsoring today's video which means that you guys will get a 20 discount on an entire year of brilliant premium if you click the link in the video description and brilliant is such a high quality platform when it comes to learning about math and science and they offer incredibly good courses on computer science they have a full course that will teach you python and literally tons of courses on computer science so i highly recommend checking them out personally i'm currently going through their course on computer memory which is great and really in depth so again i highly recommend checking that out and if you sign up using the link in my description you get a 20 discount on a year of brilliant premium so go on and sign up all right back to the video python is an abstract language and it's dynamic which means that it can't be compiled into machine code like c but it also makes python a lot more user friendly and flexible if we wanted to find an integer variable in python we can just write x equals 10 and python will just assume that this will be an integer in c however you must declare the type and write int x equals 10. now what if we had to divide this number by 3 10 divided by 3 is or gives us a float and not an integer which means that in c we would have had to typecast this variable into a float which is something that we don't have to worry about with python in python we can simply take our x variable and just divide it by three and python will then take care of all that stuff for us it will automatically cast the 10 into a float and it will give us the result of 3.333 this is what's meant by python being an interpreted language it means that we can basically just free ourselves from the constraints of having to type or write out things in the perfect way and we can just write code in a way that's very easy for us humans to understand and the interpreter of python behind the scenes will then interpret the code that we write and understand what needs to be done of course we must still respect some syntax but the language is much more relaxed and if we look closer again at our code for adding the numbers from 0 to 10 million we can use the built-in function that comes with python to do all of the work for us also note that at no point in the python code did we actually tell python anything about what type of number we would like to use it just determined on its own that we would like to work with integers but if we look at the c code we note that we had to define our numbers as integers and even more interestingly we actually had to define the sum variable to be a longer 64-bit integer to avoid an overflow error because the resulting sum variable is such a large number that it wouldn't be able to fit in a regular integer type in python on the other hand we can in general just forget about all this and just focus on the most important thing in many ways which is just writing the code and actually making the program it's made to focus on productivity and to be as pleasant as possible for a developer to work with and this is the reason why it's so easy to learn and write in python another great thing about python and the fact that it is so abstract is that you can pretty much do anything with python and developers around the world have come up with these libraries for doing pretty much whatever you could imagine if you want to do machine learning or ai use tensorflow pytorch numpy or one of the many other options if you want to make a website use django or flask you can even make games with python this leaves the final question like what about this penalty why do people still use python for computing intensive tasks like machine learning and data mining if python is inherently slow well that's because you can actually make python run really fast and this is because a lot of the packages that are run or they're made for these performance tasks where speed is important are often written in c or c plus plus which means they can almost run the much faster c code in python if we go back to our summation test and use numpy instead of the list and the built-in sum function then we can make things go a lot faster running this numpy based python code we can reduce the summation time to 0.06 seconds and this is more than 10 times faster than the regular python code though still around twice as slow as just writing in c nevertheless many of these scientists and developers can actually live with this small penalty because the vast number of libraries available and the relaxed syntax it just makes python that much more pleasant to actually develop in i hope this tour under the hood of python can help you choose what language is best for you and languages like python and javascript might not fly quite as fast as c or c plus plus but the entry bar is much lower
Info
Channel: Kalle Hallden
Views: 68,068
Rating: 4.9200444 out of 5
Keywords:
Id: VM8H1kMNHpU
Channel Id: undefined
Length: 9min 57sec (597 seconds)
Published: Fri Sep 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.