[Tutor] Slowness of Python

Alan Gauld alan.gauld at yahoo.co.uk
Thu Oct 15 05:41:17 EDT 2020


On 15/10/2020 05:57, Manprit Singh wrote:

> In this mail, I just need a reply from you. I have a question - 'Why python
> is slow in comparison to other compiled languages  like c or c++.

OK, This is a very complex one. But the first thing to say is that C/C++
are currently the fastest executing languages around. All languages are
slow in comparison to them and its not as simple as compiled v
interpreted, or static v dynamic typing.

On the other hand C++ in particular is one of the slowest languages
around when it comes to development, whereas Python is one of the
fastest. There is a direct tradeoff here. In C++ the programmer takes
responsibility for many things that Python does for you, so you have to
write a lot more code and, more importantly, think a lot more about how
the code works at the machine level. That all takes rime. Python has
been optimised for speed of development not speed of execution. For the
few cases where speed of execution is critical we have other strategies
that we can use.

C/C++ on the other hand are first and foremost systems programming
languages designed to interact with the hardware and speed, especially
predictable timing,  is of vital importance in those problem domains.

The final point is that Python is a compiled language. Your code is
compiled before it is interpreted. But where C++ compiles into native
machine code Python (like most popular modern languages - Java, C#,
Perl, Ruby etc) compiles into an intermediate language known as
byte-code and then the byte-code is interpreted. This makes the
interpreter easier to write and much faster and also makes the code
portable across machine types and OS.

An interpreter will always be slower than native machine code as
produced by a good compiler. You can get interpreters for C and C++(*)
and they are much slower than the compiled versions. But they allow
fast development and on-the-fly code testing.

(*)I'm not sure there are any modern C++ interpreters, C++ is a hugely
complex language and I haven't seen a C++ interpreter since about
1996... But C interpreters do still exist.

> There may be several reasons. The point that is coming to my mind is -
> Python is a dynamically typed language, you do not need to declare a
> datatype of the  variable when assigning a value to it . So at run time,
> when actual execution takes place, First the datatype of the variable is
> checked and then if there is any operation involved, it is also checked
> that the operation is valid for that datatype or not, then the execution
> takes place. 

To be honest I don't know the internal details of Python well enough
to answer that. But it may be that Python does not check the type
it merely checks whether the operation exists or not(so called
duck-typing)

But you are correct in thinking that dynamic typing is the biggest
factor in the speed difference between Python and statically typed
languages. The interpreter must do run-type checks before performing
an operation. So, even if you used a C interpreter, C would still
be faster than Python because of the dynamic typing!

On the other hand dynamic typing allows for much shorter code since
the programmer does not need to explicitly perform multiple type
conversions in his/her code. It also allows for polymorphic behaviour
of collections which is much more difficult to program by hand in
say C++.

Finally, as mentioned earlier, Python takes a different approach to
speed. It views high speed as a (relatively) rarely required feature
and provides alternative strategies to achieve that. Primarily it
allows functions to be written in C and then accessed from Python.
That is why most of the speed critical modules in Python are written
in C - things like the regex, pickle, math and others. So when you call
those functions they run at the same speed as C because they are C.

There are other speed boosting tools around that integrate with Python -
things like Cython and Pyrex for example. But the most important tool
in boosting speed is good design, both of code and data structures.
And Python provides a wealth of data structures that allow efficient
design of algorithms. Python also provides a profiling tool to
identify bottlenecks. So if you need your code to go faster:

1) profile it to see where the time is being spent
2) optimise the design of those parts of your program
   (This includes checking that there are no modules that
   do the same job, and tuning/changing your data
   structures to better represent the problem - sets/dicts
   are often more effective than lists for example)
3) if that's still not enough rewrite those parts in C
   or use one of the speed enhancing tools such as Cython
   or Pyrex.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list