DEV Community

Mohamed Anis MANI
Mohamed Anis MANI

Posted on

Python vs C++

As I used both Python and C/C++ and felt their strengths and weaknesses, I am annoyed when I encounter the unfair comparison between them in the social networks. The people blaming C/C++ are either persons that ignores C/C++ or that have lower marks in this module in the college.

Python is really a nice language powered by tons of libraries that offers limitless possibilities and solutions to whatever problem. However, saying that it is better than C/C++ is not always true.

This article will try to prove that the comparison between two languages should be made based on many criteria that indicate which language is better for which job.

First, Python is an interpreted language while C/C++ is a compiled language meaning that the deliverables for both languages will not be same: when writing for Python the customer expects the source code for their project, while C/C++ compiler will produces a working binary that does not require that the source code on the customer's machine. Someone will claim that Python have tools to convert source code into binary, that's true and till now I have not encountered a single tool that perform the task as it should be done, and if it exists it will needs many tweaks.

Second, Python is delivered with libraries that plays in its favour. C/C++ has its own libraries too, but in this fields it's Python the winner. In fact, with Python a simple import for a library or some features of that library suffices to resolve a problem that will needs tens of includes and their dependencies.

Talking about dependencies the Python packaging modules will ensure all the dependencies are fulfilled for one module when installing it. In the time I was using C/C++ that's feature was missing.

Third, performance side Python gives worst results because it's not designed in essence to enhance performance but to increase developer productivity, this down side is the cost of the excellent productivity of the language.

Fourth, C/C++ is meant to be as close as possible to Assembly in order to control low memory, low power, resources-limited devices. Python needs higher memory footprint and more CPU.

We shall not forget about multithreaded tasks, Python without external libraries written in C is unable to use the full multiprogramming capacities of the CPU because of its global locking.

Finally, as my article became very long I have to summarize in one sentence: choose the language that fulfils your needs by comparing each language features and capabilities and don't rely to your feeling in this matter, they won't help you.

Top comments (3)

Collapse
 
codemouse92 profile image
Jason C. McDonald • Edited

Nice rundown, overall.

However, as an expert in both languages, allow me to lend a bit of insight to a few points...

Someone will claim that Python have tools to convert source code into binary...

Nooooooooooot quite. These tools only bundle the Python interpreter binary with the Python source code, which is often (but not always) pre-converted to bytecode to save the interpreter some runtime parsing.

...till now I have not encountered a single tool that perform the task as it should be done, and if it exists it will needs many tweaks.

I've been able to ship executables created from cxFreeze, py2exe, and PyInstaller without any "tweaks"...but of course, your mileage may vary. And remember, you're not creating a formal binary in ANY case (see above).

...performance side Python gives worst results because it's not designed in essence to enhance performance but to increase developer productivity, this down side is the cost of the excellent productivity of the language.

Not quite. You're thinking of CPython, the default implementation of the langauge. Pypy, which is bootstrapped (written in itself) doesn't have this issue.

Python needs higher memory footprint and more CPU.

Again, not necessarily the case, especially with Pypy.

We shall not forget about multithreaded tasks, Python without external libraries written in C is unable to use the full multiprogramming capacities of the CPU because of its global locking.

Right now, there's some talk in Pypy about killing the global lock. Not sure what the status is, but I would be surprised if it didn't gain traction. Multiprocessing in Python is increasingly common.

Collapse
 
manimanis profile image
Mohamed Anis MANI

I want to thank you for the nice reply.

I have had very bad time once when I tried to create an executable for the Qt program I have written in CPython. Finally, when I succeeded I realized that the tool cannot package all the needed dependencies in one EXE file.

I am almost using CPython. I used IronPython with C# and I left it because it's very very slow in my machine. I used Jython too. But never used PyPy.

Collapse
 
codemouse92 profile image
Jason C. McDonald

Ye-ah, packaging a program with a GUI is sometimes quite painful with Python, especially back with Python 2. I like to think it's gotten better....ish?