Is Python faster and lighter than C++?

213,352

Solution 1

I think you're reading those stats incorrectly. They show that Python is up to about 400 times slower than C++ and with the exception of a single case, Python is more of a memory hog. When it comes to source size though, Python wins flat out.

My experiences with Python show the same definite trend that Python is on the order of between 10 and 100 times slower than C++ when doing any serious number crunching. There are many reasons for this, the major ones being: a) Python is interpreted, while C++ is compiled; b) Python has no primitives, everything including the builtin types (int, float, etc.) are objects; c) a Python list can hold objects of different type, so each entry has to store additional data about its type. These all severely hinder both runtime and memory consumption.

This is no reason to ignore Python though. A lot of software doesn't require much time or memory even with the 100 time slowness factor. Development cost is where Python wins with the simple and concise style. This improvement on development cost often outweighs the cost of additional cpu and memory resources. When it doesn't, however, then C++ wins.

Solution 2

All the slowest (>100x) usages of Python on the shootout are scientific operations that require high GFlop/s count. You should NOT use python for those anyways. The correct way to use python is to import a module that does those calculations, and then go have a relaxing afternoon with your family. That is the pythonic way :)

Solution 3

My experience is the same as the benchmarks. Python can be slow and uses more memory. I write much, much less code and it works the first time with much less debugging. Since it manages memory for me, I don't have to do any memory management, saving hours of chasing down core leaks.

What's your question?

Solution 4

Also: Psyco vs. C++.

It's still a bad comparison, since noone would do the numbercrunchy stuff benchmarks tend to focus on in pure Python anyway. A better one would be comparing the performance of realistic applications, or C++ versus NumPy, to get an idea whether your program will be noticeably slower.

Solution 5

The problem here is that you have two different languages that solve two different problems... its like comparing C++ with assembler.

Python is for rapid application development and for when performance is a minimal concern.

C++ is not for rapid application development and inherits a legacy of speed from C - for low level programming.

Share:
213,352
S. Plekhanov
Author by

S. Plekhanov

Updated on August 22, 2020

Comments

  • S. Plekhanov
    S. Plekhanov almost 4 years

    I've always thought that Python's advantages are code readibility and development speed, but time and memory usage were not as good as those of C++.

    These stats struck me really hard.

    What does your experience tell you about Python vs C++ time and memory usage?

  • S. Plekhanov
    S. Plekhanov about 15 years
    I just was confused by the results of the benchmarks. Turns out I misinterpreted them.
  • xan
    xan about 15 years
    Indeed. WHen performance is an issue, what python is good at is binding together high performance external modules, or prototyping the system and then allowing the bottlenecks (usually deep in an inner loop) to be rewritten as a C module etc.
  • Blake
    Blake about 15 years
    in other words - since numbercrunchy stuff is so much slower write it in C++ and call it from Python :-)
  • Justin Peel
    Justin Peel over 13 years
    Also, people who speak of Python being slow for serious number crunching haven't used the Numpy and Scipy modules. Python is really taking off in scientific computing these days. Of course, the speed comes from using modules written in C or libraries written in Fortran, but that's the beauty of a scripting language in my opinion.
  • ucefkh
    ucefkh over 11 years
    I asure what you said and this a link to prove it : blog.dhananjaynene.com/2008/07/…
  • Alex
    Alex about 11 years
    Regarding: c) a Python list can hold objects of different type, so each entry has to store additional data about its type. The python list is really a list of pointers to objects. In python it's the value that knows it's type, while the variable is only a pointer to the "generic value object" (therefore even numbers are immutable). So lists are not storing the types of it's contents - just pointers. You are right about the memory overhead though - python does have to store the type and other context for values of any type.
  • Quonux
    Quonux about 10 years
    if you talk about cpython..then yes, but pypy is in most cases very fast (comparable with java, 1/3 speed of java i guess), subsets of python are ever nearly as fast as c++ (see shedskin)
  • abcd
    abcd over 8 years
    @JustinPeel i question whether that's true. even when making extensive use of numpy and scipy, a large python code base is likely to have a lot of code in pure python, making things slower than C++. a python script approaches the speed of a C++ script as the percentage of its C code goes to 100, at which point it is no longer a python script. python is taking off, for sure, but not because it is as fast as C++ -- because it is easier to use.
  • Justin Peel
    Justin Peel over 8 years
    @dbliss Yes, there are many factors to consider. Yes, it is the C, C++, Cython and Fortran code underneath numpy, scipy and other libraries that really speed things up. How fast Python code is definitely depends on which parts are most computationally intensive. However, you can also speed up various parts of your code using Cython or other means if necessary. When I use C++, I also leverage code that other people have written. I don't make my own std::vector every time I want a data structure like that. CPython is written in C too so all of my Python code is really C when run with CPython.
  • Confuse
    Confuse over 7 years
    It's funny that people complain about a language instead of their own programming skills
  • Anderson Green
    Anderson Green almost 7 years
    Nowadays there are several Python-to-C++ compilers, so Python can be as fast as C++ in some cases.
  • aoeu256
    aoeu256 almost 5 years
    Those libraries are already written for Python or C or Java, so why not use a dynamic language to glue them together?
  • Yashdeep Raj
    Yashdeep Raj over 4 years
    Now a days, latest report shows that the Execution time of Python, C++ and Go is almost gets equal. Take complete detail here: auedbaki.com/article/…
  • SuperSim135
    SuperSim135 about 4 years
    If your going to use a library in python to make it faster, then you might as well use a number crunching library in c++. That way you keep the flexibility of c++ without having to write a bunch of code :)
  • Montre
    Montre about 4 years
    That’s a god-tier pointless necro. OP literally states preferring Python for readability and convenience, why would somebody directly use a language they like less, when they can get most of the performance benefits by having library authors take care of those for him? The point of using libraries is not having to do the sort of work they do better yourself, that a library happens to be a native binding is an optimization/implement detail.
  • Cris Luengo
    Cris Luengo over 3 years
    @auedbaki: "Python also comes closer once compiled using the Numba compiler." Then it is not Python though. Numba can handle only a subset of Python and Numpy. That comparison doesn't apply to arbitrary Python programs.