101

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?

11
  • 21
    So Pyhton is for most of these cases slower and uses more RAM but the source is smaller. What exactly is the problem?
    – nuriaion
    Apr 29, 2009 at 9:54
  • 2
    I guess I misinterpreted the results.
    – Alex
    Apr 29, 2009 at 10:36
  • 6
    What's really interesting is that the C++ tests are still 'better' than the C ones!
    – gbjbaanb
    Apr 29, 2009 at 10:38
  • 9
    @gbjbaanb: Doesn't surprise me. C++ has added a lot of feature that enable potentially faster code. If you know what you're doing, C++ can be ridiculously efficient, more so than C. (Of course, C++ also includes some features that hurt performance, but you don't have to use them). But the common belief that "C is faster than C++" is wrong. (and the question isn't very meaningful in the first place)
    – jalf
    Apr 29, 2009 at 15:15
  • 2
    The link is dead
    – Arn
    Feb 4, 2020 at 23:36

8 Answers 8

273

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.

9
  • 134
    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. Nov 22, 2010 at 22:42
  • 4
    I asure what you said and this a link to prove it : blog.dhananjaynene.com/2008/07/…
    – ucefkh
    Dec 25, 2012 at 2:29
  • 3
    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.
    – Alex
    May 22, 2013 at 9:13
  • 1
    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)
    – Quonux
    Apr 19, 2014 at 17:46
  • 2
    @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.
    – abcd
    Oct 15, 2015 at 4:03
143

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 :)

1
26

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?

1
  • I just was confused by the results of the benchmarks. Turns out I misinterpreted them.
    – Alex
    Apr 29, 2009 at 10:35
15

Source size is not really a sensible thing to measure. For example, the following shell script:

cat foobar

is much shorter than either its Python or C++ equivalents.

3
  • 40
    And much easier to maintain that the longer Python or C++ versions, too. I argue the source code size does matter, and for certain simple tasks, terse shell scripts are good.
    – S.Lott
    Apr 29, 2009 at 10:23
  • I also believe that source code size matters a lot, and for some tasks, Bash is the right tool for the job. See a nice example comparing a simple bash script to python here: innolitics.com/articles/programming-languages/… (you need to scroll down a bit). I think it is a slightly more sophisticated example than cat footer.
    – jdg
    Feb 2, 2018 at 23:00
  • This thread is about code speed/size, not maintainability.
    – Noob
    Sep 13, 2021 at 9:28
8

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.

3
  • 2
    in other words - since numbercrunchy stuff is so much slower write it in C++ and call it from Python :-)
    – igouy
    Apr 30, 2009 at 17:20
  • 1
    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 :) May 15, 2020 at 20:02
  • 1
    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.
    – millimoose
    May 30, 2020 at 9:33
6

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.

0
4

It's the same problem with managed and easy to use programming language as always - they are slow (and sometimes memory-eating).

These are languages to do control rather than processing. If I would have to write application to transform images and had to use Python too all the processing could be written in C++ and connected to Python via bindings while interface and process control would be definetely Python.

1
  • Those libraries are already written for Python or C or Java, so why not use a dynamic language to glue them together?
    – aoeu256
    Jul 11, 2019 at 16:13
2

I think those stats show that Python is much slower and uses more memory for those benchmarks - are you sure you're reading them the right way up?

In my experience, which is mostly with writing network- and file-system-bound programs in Python, Python isn't significantly slower in any way that matters. For that kind of work, its benefits outweigh its costs.

1
  • 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.
    – xan
    Apr 29, 2009 at 11:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.