I just want to know why do Machine Learning engineers and AI programmers use languages like python to perform AI task and not C++ even though C++ is technically a more powerful language than python.

  • 2
    $\begingroup$ This answer will help $\endgroup$ – Ugnes Apr 27 '18 at 15:23
  • $\begingroup$ Welcome to AI! I've slightly edited the title of the question. High-level, Python has light syntax and is an interpreted language, which means tweaking and testing with no compiling. Python also allows bitwise operations. $\endgroup$ – DukeZhou Apr 27 '18 at 16:10

You don't need a powerful language for programming AI. Most of the developers are using libraries like Keras, Torch, Caffe, Watson, TensorFlow, etc. Those libraries are highly optimized and handle all the though work, they are built with high performance languages, like C. Python is just there to describe the neural network layers, load data, launch the processing and display results. Using C++ instead would give barely no performance improvement, but would be harder for non-developers as it require to care for memory management. Also, several AI people may not have a very solid programming or computer science background.

Another similar example would be game development, where the engine is coded in C/C++, and, often, all the game logic scripted in a high level language.

  • 1
    $\begingroup$ If I am interested in developing and working with machine learning and nlp for the hardware field.. So can I use C++ for AI $\endgroup$ – Mark ellon Apr 27 '18 at 8:20
  • 1
    $\begingroup$ @AnishGupta: Yes you can, provided hardware supports the necessary processing (neural networks specifically are very processor intensive). TensforFlow is in fact natively C++ (Python bindings are an addition), and here is the API: tensorflow.org/api_docs/cc $\endgroup$ – Neil Slater Apr 27 '18 at 9:00
  • $\begingroup$ "most AI profiles come from data science field". Data science is not even a well-defined field, so your sentence may not be understood or misunderstood. $\endgroup$ – nbro Nov 6 '18 at 22:14
  • 1
    $\begingroup$ "Using C++ instead would give barely no performance improvement, but would be harder for non-developers as it require to care for memory management." You don't need to care about memory management with C++ if you write it well. $\endgroup$ – Jérémy Blain Nov 7 '18 at 8:12
  • $\begingroup$ AI programming is not only about using existing libraries like Keras and Torch. A neural network can be programmed from scratch without external dependendies. Examples for a 3-layer perceptron are available in the internet and the sigmoid activation function fits into 4 lines of code. I would say, that implementing a neural network without existing libraries is the easier way to understand the topic. $\endgroup$ – Manuel Rodriguez Nov 7 '18 at 10:11

C++ is actually one of the most popular languages used in the AI/ML space. Python may be more popular in general, but as others have noted, it's actually quite common to have hybrid systems where the CPU intensive number-crunching is done in C++ and Python is used for higher level functions.

Just to illustrate:




It depends how flexible it needs to be: if you have a fully-fledged system ready for production, which is not going to need much adjusting, then C++ (or even C) might be fine. You need to put a lot of time into building the software, but then it should run pretty fast.

However, if you're still experimenting with settings and parameters, and maybe need to adjust the architecture, then C++ will be clumsy to work with. You need a language like Python which makes it easier to change things. Changing the code is easier, as you can generally code faster in languages like Python. The price you pay is that the software does usually not perform as well.

You need to decide how that trade-off works best for you. It is usually better to spend less time on coding, and not worry too much about longer run-time. If you take a day less to get your code done, that's a lot of time the C-coded version needs to catch up. Most of the time it's just not worth it.

A common approach seems to be hybrid systems, where core libraries are implemented in C/C++, as they don't need much changing, and the front-end/glue/interfaces are in Python, as there you need flexibility and speed is not that critical.

This is not an issue specific to AI, by the way, but a general question of interpreted vs compiled languages. With AI a lot of systems are still focused on research rather than application, and that is where speed of development trumps speed of execution.

  • 1
    $\begingroup$ If I do it with C++ then can I get fast execution than python and what's about the performance level because it is stated that : C++ is 400 times faster than python $\endgroup$ – Mark ellon Apr 27 '18 at 14:35
  • 1
    $\begingroup$ That general figure doesn't mean anything -- it really depends on the exact application. I think 400 times is probably far too optimistic for most situations. But the point is that if it takes you 500x longer to code (and debug!) the software, then even a 400x improvement in speed would still be slower! Obviously the numbers are a bit random, but don't underestimate the time it takes you to get your program right. Often you'll find that the increase in speed is more than offset by faster development time. $\endgroup$ – Oliver Mason Apr 27 '18 at 14:39
  • 1
    $\begingroup$ I have tried to code a neural net using tensorflow libraries in python and C++ .. and i found that : the neural network algorithm which was coded in C++ runs 280X times faster than python in which same libraries and same algorithm was used $\endgroup$ – Mark ellon Apr 27 '18 at 14:46
  • 1
    $\begingroup$ So why not to use C++ for Machine learning algorithms implementation rather than python $\endgroup$ – Mark ellon Apr 27 '18 at 14:47
  • 1
    $\begingroup$ See my answer above... $\endgroup$ – Oliver Mason Apr 27 '18 at 14:47

Software development for AI applications can be separated into programming itself and prototyping. C/C++ is a great language to create the application because it runs very fast and can be delivered as libraries for mainstream operating systems. A precompiled C/C++ application is the gold standard if somebody want's to deploy a turnkey appliance.

C++ has a major problem, before a program can be compiled with GCC or the LLVM compiler somebody needs to know which algorithm he needs. C++ can execute a given sourcecode, and provides efficient commands but in which way the array has to be filled and which for loops are needed in the code is unclear. This question fits into the prototyping step which comes before the application gets programmed. The problem is not how to build a compiled application and deliver this as an operating system package, the problem is to play with different AI algorithm, build some gui prototypes and discuss with team members the progress.

The number one gui prototyping language which is based on scripting programming and provides near-pseudocode capabilities was invented by Guido van Rossum. It never replaced C++, but it creates a new kind of domain. There is a need for a prototyping step before the software gets implemented, especially in the innovative domain of Artificial Intelligence.

To explain why Python is superior to C++ we have to try to build a software prototype with C++. Is it possible to use that language for fast implementing a gui application? No C++ was designed not as a prototyping language with fast edit cycles, but as a solid rock for system programmers. That means, if the prototype is already working, if the algorithm is fixed and if the documentation was written it make sense to reprogram the code in C++. That means, a given Python prototype is converted into C++ and gets delivered to existing operating systems. But for the pre-step which has to do with writing papers, discussing alternatives and managing innovations, Python is the better choice.


You claim that

C++ is technically a more powerful language than python.

But that claim is wrong. Remember that a programming language is a specification (often some document written in English). For example, n3337 is a late draft of the C++ specification.

Theoretically, both C++ and Python are Turing-complete (on purpose) programming languages.

And Python is as expressive as C++ is. I cannot name a programming language feature that Python has but not C++ (except those related to reflection; see also this answer and be aware of dlopen - see my manydl.c program -, of LLVM, of libgccjit, of libbacktrace, and consider some meta-programming approach with them, à la Bismon or like J.Pitrat's blog advocates it).

Maybe you think of a programming language as the software implementing it. Then Python is as expressive as C++ is (and seems easier to learn, but that is an illusion; see http://norvig.com/21-days.html for more about that illusion). Python and C++ have a quite similar semantics, even if their syntax is very different. Their type system is very different.

Observe that sadly, many recent major machine learning libraries (such as TensorFlow or Gudhi) are in practice easier to use in Python than in C++. But you can use TensorFlow or Gudhi from C++ code since TensorFlow and Gudhi are mostly coded in C++ and both provide and document a C++ API (not just a Python one).

C++ enables multi-threaded programming, but the usual Python implementation has its GIL, is bytecoded, so is significantly slower than C++ (which is usually compiled by optimizing compilers such as GCC or Clang; however you could find C++ interpreters, e.g. Cling). Some experimental implementations of Python are JIT-compiled and without GIL. But these are not mature: I recommend investing a million euros to increase their TRL.

Observe also that C++ is much more difficult to learn than Python. Even with a dozen years of C++ programming experience, I cannot claim to really know most of C++.

Sadly, most recent books teaching AI software engineering (e.g. this one or that one) use Python (not C++) for their examples. I actually want more recent AI books using C++ !

BTW, I program open source software (like this one, or the obsolete GCC MELT) using AI techniques, but they don't use Python. My approach to AI applications is to start designing some DSL in them.


protected by nbro Sep 28 at 19:37

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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