'Why python is slow?' this is the question
that comes up from
people who have worked with compile
languages like Java and c++
sometimes people also argue
javascript is an interpreter language
and still it is faster than python.
I asked this question to one of the
members of python steering council.
python studying council is a committee
of five people who decides
the future direction of python language
these are the creators of python
language
and I will quickly summarize what we
discuss in that call
in this video point number one is when
you are saying python is slow
are you using right module and right
approach?
for example when you are doing heavy
number crunching vector and matrix
operation you should be using numpy and
pandas
here i am going to do a quick demo of
comparing numpy performance with plain
python list.
here i have created two big list hundred
thousand elements
each using plain python list
and I am going to now divide this to
list element by element
so that I get price to earning ratio
this gives 8.35 millisecond performance
but if you I use numpy for the same
thing you see I have created numpy
arrays.
I get 94 microsecond
which means numpy is 88 times faster.
So when it comes to big metrics and
operations and
vector operations numpy and pandas
perform the best and that's the reason
they are used heavily in machine
learning
you see that using numpy speeds of the
performance
and if you are using python list then it
was not a
python's problem it was your problem you
are not aware about
the right approach that you should be
using in your program. Now the reason
numpy's and pandas are faster is
they underneath execute in c or
c++
so python just provides a wrapper API.
You make like one line call
underneath you get c++
performance and that's the reason
python is used so much in machine
learning
most of the people who are doing machine
learning they use python
they don't know don't use javascript or
they don't use c++
so just think about it. Machine learning
is so much computation intensive.
If python was slow why would people use
it for machine learning?
Point number two is let's take about
plain python so if you are writing a
program in plain python
of course it's gonna be slower than
compile languages like Java and c++
for obvious reasons python is a
high level language it gives you
convenience- rapid prototyping
so to get that benefit you have to
compromise on the speed
now why javascript being an interpreted
language is faster because
think about Google Chrome for example
javascript
is built into a browser. Now Google as a
company wants people to use Google
Chrome so Google put
so much money in making Javascript
faster
javascript is a language specification
they have
this v8 engine v8
the Google Chrome runs on a v8
Javascript engine which is super fast
so google puts tons of money in making
that
faster because they want people to use
Google Chrome.
so when big companies like google is
putting a lot of money into some
language
of course you will get performance
whereas python
yeah people are work putting money into
it
but no company has a proprietary python
product like
Coogle chrome so they contribute
little bit. So python doesn't get
as much funding as javascript. Having
said that
there are a force being made to make the
plain python performance
better. And you can listen to that whole
conversation I'm going gonna put the
link of that conversation with python
council member in the video description
below.
But overall if you're using the right
set of libraries like numpy's and pandas
you'll probably not have
complaints about speed sometimes your
program is slow
is slow because you are not using the
right design pattern
or you are using some for loop
unnecessarily so whenever you
you think your program is the first try
to analyze
if there are any design problems in your
code. Try to fix them
then try to use the right set of
libraries like numpas and pandas.
90 to 95 of the time you will not have a
complaint that python is slow
I hope this video helps with your
question. If you
like this video give it a thumbs up. If
you have any other question
post in the comment below.