Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Python faster and lighter than C++? [closed]

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?

like image 595
Alex Avatar asked Apr 29 '09 09:04

Alex


People also ask

Which is faster Python or C?

C is a faster language compared to Python as it is compiled. Python programs are usually slower than C programs as they are interpreted. In C, the type of the various variables must be declared when they are created, and only values of those particular types must be assigned to them.

Is Python lighter than C++?

Clearly, C++ is much faster than Python in running the same algorithm and instructions. It is not a surprise to most programmers and data scientists, but the example shows that the difference is significant.

Which is faster C or C++ or Python?

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.

Why is Python slow compared to C?

Internally Python code is interpreted during run time rather than being compiled to native code hence it is a bit slower. Running of Python script v/s running of C/C++ code: Python: First it is compiled into Byte Code. This Byte Code is then interpreted and executed by the PVM (Python Virtual Machine).


2 Answers

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.

like image 72
moinudin Avatar answered Sep 19 '22 18:09

moinudin


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

like image 32
Tim Lin Avatar answered Sep 18 '22 18:09

Tim Lin