Answers.Body: <p>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 low level libraries are highly optimized and handle all the tough work. They are built with high-performance languages, like C, C++. Python is just there for high level task like describing the neural network layers, load data, launch the processing, and display results. </p>
  
  <p>Using C++ for high level task instead of Python would give barely any performance improvement, but it would be harder for non-developers as it requires to care for memory management. Also, several AI people may not have a very solid programming or computer science background.</p>
  
  <p>Another similar example would be game development, where the engine is coded in C/C++, and, often, all the game logic scripted in a higher-level language.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-04-27T08:07:26.937Z
Answers.LastActivityDate: 2020-06-04T21:10:29.387Z
Answers.LastEditDate: 2020-06-04T21:10:29.387Z
Answers.UserDisplayName: bokan
Answers.Body: <p>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.</p>
  
  <p>Just to illustrate:</p>
  
  <p><a href="http://mloss.org/software/language/c__/" rel="noreferrer">http://mloss.org/software/language/c__/</a></p>
  
  <p><a href="http://mloss.org/software/language/python/" rel="noreferrer">http://mloss.org/software/language/python/</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-07-11T11:20:32.563Z
Answers.Text: Good point, however the links do not work anymore: they both forward to the same list of results, not filtering per language. Next time, please provide a textual list of examples for both cases, and only use the link for historical reference. Answers on SO/SE should not strongly rely on external links.
Answers.UserDisplayName: alelom
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-27T13:45:14.140Z
Answers.LastActivityDate: 2018-04-27T13:45:14.140Z
Answers.UserDisplayName: mindcrime
Answers.Body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-27T10:53:41.543Z
Answers.LastActivityDate: 2018-04-27T10:53:41.543Z
Answers.UserDisplayName: Oliver Mason
Answers.Body: <p>You claim that</p>
  <blockquote>
  <p>C++ is technically a more powerful language than python.</p>
  </blockquote>
  <p>But that claim is wrong (or does not mean much). Remember that <strong>a programming language is a <em>specification</em></strong> (often some document written in English). For example, <a href="https://github.com/cplusplus/draft/blob/master/papers/n3337.pdf" rel="nofollow noreferrer">n3337</a> is a late draft of the C++ specification. I don't like Python, but it does seems as powerful than C++ (even if C++ <em>implementations</em> are generally faster than Python ones): what a good Python programmer can code well in Python, another good C++ programmer can code well in C++ and vice versa.</p>
  <p>Theoretically, both C++ and Python are <a href="https://en.wikipedia.org/wiki/Turing_completeness" rel="nofollow noreferrer">Turing-complete</a> (on purpose) programming languages.</p>
  <p>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 <a href="https://en.wikipedia.org/wiki/Reflection_(computer_programming)" rel="nofollow noreferrer">reflection</a>; see also <a href="https://ai.stackexchange.com/a/2237/3335">this</a> answer and be aware of <a href="https://en.wikipedia.org/wiki/Dynamic_loading" rel="nofollow noreferrer"><code>dlopen</code></a> - see my <a href="https://github.com/bstarynk/misc-basile/blob/master/manydl.c" rel="nofollow noreferrer">manydl.c</a> program -, of <a href="https://llvm.org/" rel="nofollow noreferrer">LLVM</a>, of <a href="https://gcc.gnu.org/onlinedocs/jit/" rel="nofollow noreferrer">libgccjit</a>, of <a href="https://github.com/ianlancetaylor/libbacktrace" rel="nofollow noreferrer">libbacktrace</a>, and consider some <a href="https://en.wikipedia.org/wiki/Metaprogramming" rel="nofollow noreferrer">meta-programming</a> approach with them, à la <a href="https://github.com/bstarynk/bismon/" rel="nofollow noreferrer">Bismon</a> or like <a href="http://bootstrappingartificialintelligence.fr/WordPress3/" rel="nofollow noreferrer">J.Pitrat's blog</a> advocates it).</p>
  <p>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 <a href="http://norvig.com/21-days.html" rel="nofollow noreferrer">http://norvig.com/21-days.html</a> for more about that illusion). Python and C++ have a quite similar <a href="https://en.wikipedia.org/wiki/Semantics_(computer_science)" rel="nofollow noreferrer">semantics</a>, even if their syntax is very different. Their <a href="https://en.wikipedia.org/wiki/Type_system" rel="nofollow noreferrer">type system</a> is very different.</p>
  <p>Observe that sadly, many recent <strong>major machine learning libraries</strong> (such as <a href="https://www.tensorflow.org/" rel="nofollow noreferrer">TensorFlow</a> or <a href="http://gudhi.gforge.inria.fr/" rel="nofollow noreferrer">Gudhi</a>, both mostly coded in C++) are <em>in practice</em> easier to use in Python than in C++. But you can use TensorFlow or Gudhi from C++ code since <strong>TensorFlow and Gudhi are mostly coded in C++</strong> and both provide and document a C++ <a href="https://en.wikipedia.org/wiki/Application_programming_interface" rel="nofollow noreferrer">API</a> (not just a Python one).</p>
  <p>C++ enables <a href="https://en.cppreference.com/w/cpp/thread" rel="nofollow noreferrer">multi-threaded programming</a>, but the usual Python implementation has its <a href="https://en.wikipedia.org/wiki/Global_interpreter_lock" rel="nofollow noreferrer">GIL</a>, is <a href="https://opensource.com/article/18/4/introduction-python-bytecode" rel="nofollow noreferrer">bytecoded</a>, so is significantly slower than C++ (which is usually compiled by <a href="https://en.wikipedia.org/wiki/Optimizing_compiler" rel="nofollow noreferrer">optimizing compilers</a> such as <a href="https://gcc.gnu.orrg/" rel="nofollow noreferrer">GCC</a> or <a href="https://clang.llvm.org/" rel="nofollow noreferrer">Clang</a>; however you could find C++ interpreters, e.g. <a href="https://github.com/root-project/cling" rel="nofollow noreferrer">Cling</a>). Some experimental implementations of Python are <a href="https://en.wikipedia.org/wiki/Just-in-time_compilation" rel="nofollow noreferrer">JIT-compiled</a> and without GIL. But these are not mature: I recommend investing a million euros to increase their <a href="https://en.wikipedia.org/wiki/Technology_readiness_level" rel="nofollow noreferrer">TRL</a>.</p>
  <p>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++.</p>
  <p>Sadly, most recent books teaching AI software engineering (e.g. <a href="https://rads.stackoverflow.com/amzn/click/com/1449369413" rel="nofollow noreferrer" rel="nofollow noreferrer">this</a> one or <a href="https://mitpress.mit.edu/books/introduction-deep-learning" rel="nofollow noreferrer">that</a> one) use Python (not C++) for their examples. I actually want more recent AI books using C++ !</p>
  <p>BTW, I program open source software (like <a href="https://github.com/bstarynk/bismon/" rel="nofollow noreferrer">this</a> one, or the obsolete <a href="http://starynkevitch.net/Basile/gcc-melt/" rel="nofollow noreferrer">GCC MELT</a>) using AI techniques, but they don't use Python. My approach to AI applications is to start designing some <a href="https://en.wikipedia.org/wiki/Domain-specific_language" rel="nofollow noreferrer">DSL</a> in them.</p>
  <p>Some AI approaches involve metaprogramming, e.g. generating some (or most, or even all) the code of a system by itself. J.Pitrat (he passed away in October 2019) pioneered this approach. See <a href="http://bootstrappingartificialintelligence.fr/WordPress3/" rel="nofollow noreferrer">his blog</a>, his CAIA system, read his <a href="https://rads.stackoverflow.com/amzn/click/com/1848211015" rel="nofollow noreferrer" rel="nofollow noreferrer"><em>Artificial Beings, the conscience of a couscious machine</em></a> book (ISBN 978-1848211018) and the <a href="http://refpersys.org/" rel="nofollow noreferrer">RefPerSys</a> project (whose ambition is to generate most -and hopefully all- of its C++ code).</p>
  <p>On operating systems such as Linux you could in practice generate C++ (or C) code at runtime and compile it (using <a href="http://gcc.gnu.org/" rel="nofollow noreferrer">GCC</a>) into a <a href="https://en.wikipedia.org/wiki/Plug-in_(computing)" rel="nofollow noreferrer">plugin</a>, then later <a href="https://man7.org/linux/man-pages/man3/dlopen.3.html" rel="nofollow noreferrer">dlopen(3)</a> that generated plugin, and retrieve function pointers by their name using <a href="https://man7.org/linux/man-pages/man3/dlsym.3.html" rel="nofollow noreferrer">dlsym(3)</a>. See the <a href="https://github.com/bstarynk/misc-basile/blob/master/manydl.c" rel="nofollow noreferrer">manydl.c</a> example (on a powerful desktop in 2020, you would be able to generate and load half a million of plugins, if you run that example several days). With <a href="https://man7.org/linux/man-pages/man3/dladdr.3.html" rel="nofollow noreferrer">dladdr(3)</a> and Ian Taylor's <a href="https://github.com/ianlancetaylor/libbacktrace" rel="nofollow noreferrer">libbacktrace</a>, you can also inspect some of the <a href="https://en.wikipedia.org/wiki/Call_stack" rel="nofollow noreferrer">call stack</a>.</p>
  <p>AFAIK major corporations such as Google use C++ internally for most of their AI-related code. Look also into <a href="https://en.wikipedia.org/wiki/MILEPOST_GCC" rel="nofollow noreferrer">MILEPOST GCC</a> or the H2020 <a href="https://www.decoder-project.eu/" rel="nofollow noreferrer">Decoder project</a> for an application of machine learning techniques to compilers. See also <a href="https://www.hipeac.net/" rel="nofollow noreferrer">HIPEAC</a>.</p>
  <p>Of course, you can code AI software in <a href="https://www.haskell.org/" rel="nofollow noreferrer">Haskell</a>, in <a href="https://en.wikipedia.org/wiki/Common_Lisp" rel="nofollow noreferrer">Common Lisp</a> (e.g. with <a href="https://sbcl.org/" rel="nofollow noreferrer">SBCL</a>), or in <a href="https://ocaml.org/" rel="nofollow noreferrer">Ocaml</a>. Many <a href="https://en.wikipedia.org/wiki/Machine_learning" rel="nofollow noreferrer">machine learning</a> frameworks can be called from them. Number crunching libraries could use <a href="https://en.wikipedia.org/wiki/OpenCL" rel="nofollow noreferrer">OpenCL</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-29T06:39:24.253Z
Answers.LastActivityDate: 2020-09-04T08:46:53.413Z
Answers.LastEditDate: 2020-09-04T08:46:53.413Z
Answers.UserDisplayName: Basile Starynkevitch
Body: <p>I just want to know why do machine learning engineers and AI programmers use languages like Python to perform AI tasks and not C++, even though C++ is technically a more powerful language than Python.</p>
  
Tags: <machine-learning><comparison><python><programming-languages><c++>
Title: Why does C++ seem less widely used than Python in AI?
UserDisplayName: Mark ellon
ThreadText: # Why does C++ seem less widely used than Python in AI?
  
  (Asked by: username_0 on 2018-04-27)
  
  I just want to know why do machine learning engineers and AI programmers use languages like Python to perform AI tasks and not C++, even though C++ is technically a more powerful language than Python.
  
  **Tags:** machine-learning, comparison, python, programming-languages, c++
  
  ---
  
  ## 4 answers
  
  ### Answer by username_1 on 2018-04-27. Score: 35
  
  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 low level libraries are highly optimized and handle all the tough work. They are built with high-performance languages, like C, C++. Python is just there for high level task like describing the neural network layers, load data, launch the processing, and display results.
  
  Using C++ for high level task instead of Python would give barely any performance improvement, but it would be harder for non-developers as it requires 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 higher-level language.
  
  
  ---
  
  ### Answer by username_2 on 2018-04-27. Score: 16
  
  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:
  
  <http://mloss.org/software/language/c__/>
  
  <http://mloss.org/software/language/python/>
  
  > **Comments:**
  > **username_3**: Good point, however the links do not work anymore: they both forward to the same list of results, not filtering per language. Next time, please provide a textual list of examples for both cases, and only use the link for historical reference. Answers on SO/SE should not strongly rely on external links.  
  
  ---
  
  ### Answer by username_4 on 2018-04-27. Score: 5
  
  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.
  
  
  ---
  
  ### Answer by username_5 on 2019-09-29. Score: 1
  
  You claim that
  
  > C++ is technically a more powerful language than python.
  
  But that claim is wrong (or does not mean much). Remember that **a programming language is a *specification*** (often some document written in English). For example, [n3337](https://github.com/cplusplus/draft/blob/master/papers/n3337.pdf) is a late draft of the C++ specification. I don't like Python, but it does seems as powerful than C++ (even if C++ *implementations* are generally faster than Python ones): what a good Python programmer can code well in Python, another good C++ programmer can code well in C++ and vice versa.
  
  Theoretically, both C++ and Python are [Turing-complete](https://en.wikipedia.org/wiki/Turing_completeness) (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](https://en.wikipedia.org/wiki/Reflection_(computer_programming)); see also [this](https://ai.stackexchange.com/a/2237/3335) answer and be aware of [`dlopen`](https://en.wikipedia.org/wiki/Dynamic_loading) - see my [manydl.c](https://github.com/bstarynk/misc-basile/blob/master/manydl.c) program -, of [LLVM](https://llvm.org/), of [libgccjit](https://gcc.gnu.org/onlinedocs/jit/), of [libbacktrace](https://github.com/ianlancetaylor/libbacktrace), and consider some [meta-programming](https://en.wikipedia.org/wiki/Metaprogramming) approach with them, à la [Bismon](https://github.com/bstarynk/bismon/) or like [J.Pitrat's blog](http://bootstrappingartificialintelligence.fr/WordPress3/) 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](https://en.wikipedia.org/wiki/Semantics_(computer_science)), even if their syntax is very different. Their [type system](https://en.wikipedia.org/wiki/Type_system) is very different.
  
  Observe that sadly, many recent **major machine learning libraries** (such as [TensorFlow](https://www.tensorflow.org/) or [Gudhi](http://gudhi.gforge.inria.fr/), both mostly coded in C++) 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](https://en.wikipedia.org/wiki/Application_programming_interface) (not just a Python one).
  
  C++ enables [multi-threaded programming](https://en.cppreference.com/w/cpp/thread), but the usual Python implementation has its [GIL](https://en.wikipedia.org/wiki/Global_interpreter_lock), is [bytecoded](https://opensource.com/article/18/4/introduction-python-bytecode), so is significantly slower than C++ (which is usually compiled by [optimizing compilers](https://en.wikipedia.org/wiki/Optimizing_compiler) such as [GCC](https://gcc.gnu.orrg/) or [Clang](https://clang.llvm.org/); however you could find C++ interpreters, e.g. [Cling](https://github.com/root-project/cling)). Some experimental implementations of Python are [JIT-compiled](https://en.wikipedia.org/wiki/Just-in-time_compilation) and without GIL. But these are not mature: I recommend investing a million euros to increase their [TRL](https://en.wikipedia.org/wiki/Technology_readiness_level).
  
  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](https://rads.stackoverflow.com/amzn/click/com/1449369413) one or [that](https://mitpress.mit.edu/books/introduction-deep-learning) 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](https://github.com/bstarynk/bismon/) one, or the obsolete [GCC MELT](http://starynkevitch.net/Basile/gcc-melt/)) using AI techniques, but they don't use Python. My approach to AI applications is to start designing some [DSL](https://en.wikipedia.org/wiki/Domain-specific_language) in them.
  
  Some AI approaches involve metaprogramming, e.g. generating some (or most, or even all) the code of a system by itself. J.Pitrat (he passed away in October 2019) pioneered this approach. See [his blog](http://bootstrappingartificialintelligence.fr/WordPress3/), his CAIA system, read his [*Artificial Beings, the conscience of a couscious machine*](https://rads.stackoverflow.com/amzn/click/com/1848211015) book (ISBN 978-1848211018) and the [RefPerSys](http://refpersys.org/) project (whose ambition is to generate most -and hopefully all- of its C++ code).
  
  On operating systems such as Linux you could in practice generate C++ (or C) code at runtime and compile it (using [GCC](http://gcc.gnu.org/)) into a [plugin](https://en.wikipedia.org/wiki/Plug-in_(computing)), then later [dlopen(3)](https://man7.org/linux/man-pages/man3/dlopen.3.html) that generated plugin, and retrieve function pointers by their name using [dlsym(3)](https://man7.org/linux/man-pages/man3/dlsym.3.html). See the [manydl.c](https://github.com/bstarynk/misc-basile/blob/master/manydl.c) example (on a powerful desktop in 2020, you would be able to generate and load half a million of plugins, if you run that example several days). With [dladdr(3)](https://man7.org/linux/man-pages/man3/dladdr.3.html) and Ian Taylor's [libbacktrace](https://github.com/ianlancetaylor/libbacktrace), you can also inspect some of the [call stack](https://en.wikipedia.org/wiki/Call_stack).
  
  AFAIK major corporations such as Google use C++ internally for most of their AI-related code. Look also into [MILEPOST GCC](https://en.wikipedia.org/wiki/MILEPOST_GCC) or the H2020 [Decoder project](https://www.decoder-project.eu/) for an application of machine learning techniques to compilers. See also [HIPEAC](https://www.hipeac.net/).
  
  Of course, you can code AI software in [Haskell](https://www.haskell.org/), in [Common Lisp](https://en.wikipedia.org/wiki/Common_Lisp) (e.g. with [SBCL](https://sbcl.org/)), or in [Ocaml](https://ocaml.org/). Many [machine learning](https://en.wikipedia.org/wiki/Machine_learning) frameworks can be called from them. Number crunching libraries could use [OpenCL](https://en.wikipedia.org/wiki/OpenCL).
  
  
  ---
  
  