top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Why to use C++ instead of python?

0 votes
674 views

I consider myself a python programmer, although C++ was one of the first languages I learned (not really deeply and long time ago).

Now I decided to retake C++, to broaden my view of the business. However, as I progress in learning C++, I cannot take out of my head one question

Why to use C++ instead of python?

It is not ranting against C++. I was/am looking for small-medium projects to exercise my C++ skills. But I'm interested in a "genuine" C++ project: some task where C++ is really THE language (and where python is actually a bad as initial choice).

The usual argument in favor of C++ (when comparing to python) is performance. But I'm convinced that, in general, the right approach is "python-profiling-(extension/numpy/Cython/...)". At least for a python
programmer. I might be wrong, though.

This is, perhaps, a bit off-topic, but I really want to know the thoughts of experienced python programmers on it.

posted Aug 21, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

No, it's a fair question.

The fact is, there's not a huge amount of reason left. If you're linking against a C++ API, you may find it easiest to do the whole program in C++, rather than use something like Cython (which, as far as I'm aware, is C-only) or write a two-part project. And obviously if you have an existing C++ codebase, then porting it has costs, and maintaining it is probably better. But for the most part, I would strongly recommend starting a project in a high level language like Python unless there's a really compelling reason to do otherwise. C++ has, of late, been growing a number of features that belong in higher level languages; but if you want those sorts of features, why not just grab Python or Pike or something and save yourself the trouble?

One possible advantage is compactness. Once you've compiled a C++ program, you don't have to distribute as much stuff with it as an entire Python interpreter. Also, true compilation obscures your source
code a lot better than any attempt at a mangled Python would, so if you're trying to demonstrate to upper management that your code isn't being given away, that might be an advantage. (But frankly, even that isn't all that useful. The only way to truly stop people from using your code is to not give it to them in any form, which these days generally means putting it on a server and providing web browser access. And that works just fine for Python code.)

There are reasons for using C, of course. I'm not sure whether your question is about C++ specifically, or also about C. But in my opinion, C is for writing high level languages in, and applications should be written in something else. It's like stack-based programming - sure, it's efficient and all, but it's not something you want to spend all day debugging. (If you look at CPython bytecode disassembly, you'll see that the interpreter's actually stack-based; but we don't have to worry about keeping the stack straight, we just write nice clean Python code.) Interfacing with C-level libraries can be done with an absolute minimum of low-level code (probably with Cython), and the main application logic can still be in Python.

Let's suppose you're starting a greenfield project, and either Python or C++ would have been the perfect language, but you chose wrongly. What are the consequences? If you chose C++, you have a much heavier development and maintenance cost, and Python would have been good enough, so you get no significant benefit - basically, you miss out on the ease of coding that Python offers you. If you chose Python, what you now have is a proof-of-concept that you can use to prove the correctness of any rewrites (just get a good test suite going and then use the same tests for both engines), at a relatively low development cost. Considering that the cost of erring on Python's side is lower AND the likelihood of Python being correct is higher, I would say that you can safely bet on Python for a new project, and leave C++ until you have some really good reason for taking it up :)

answer Aug 21, 2014 by Ahmed Patel
0 votes

For my day job, I chose Qt on C++ for a classic desktop app that needs to be deployed on Windows (among other platforms) with an installation package that is as small as possible.

All I need to do deployment-wise is to create an NSIS script putting a couple of DLL's and my executable in a folder in ProgramFiles and a shortcut in start menu. The full package is 5 megs and the update archive is pushing half a megabyte.

I was also back to C++ after a number of years of exclusive web dev with Python and Javascript. C++11 is just sweet, I'd never imagined I'd enjoy doing non-computer-science work with C++.

answer Aug 21, 2014 by Ankit
Similar Questions
+2 votes

Below instruction will start CPU stress with maximum of 100%,

while(1);

Below instructions will start CPU stress with near to 0%.

while(1)
     sleep(1);

Is there any way to control this? I mean what if i want to start CPU stress of >100% and what if want to start CPU stress with particular number?

Can any one help in programming that? I am looking for C/C++/Python solution on Linux...

+7 votes
#include<stdio.h>

int &fun()
{
   static int x;
   return x;
}   

int main()
{
   fun() = 10;
   printf(" %d ", fun());

   return 0;
}

It is fine with c++ compiler while giving error with c compiler.

...