DEV Community

Cover image for Why is artificial intelligence driven by Python and not C++?
Ibne Nahian
Ibne Nahian

Posted on

Why is artificial intelligence driven by Python and not C++?

In short, for Productivity.

If you want to know further , you have to go through all of it.
Python is not the one and only solution for Artificial Intelligence , Data Science , Deep Learning , Neural Network etc. Rather C++ , .NET , Java , JavaScript , Prolog , Julia are some frequently used Ecosystems for those. But we heard too much about Python that nowadays we assume Artificial Intelligence , Data Science , Deep Learning - these things only run on the top of Python. Truth is far from this.

Basically there are two types of languages.

  • Performant Language e.g. C , C++ , Asm , B.
  • Productive Language e.g. Python , JS , Java , Kotlin , Swift.

Python is a Glue Language which combines the higher level API's of different libraries & makes easy to use. Under the hood , those Libraries are mostly written in C/C++. The history of using Glue Languages for scientific research are more older than we actually think. Programmers used to write their code using Perl & TCL back. Python is more popular nowadays.

Now you might ask

"Why Glue Languages ?"

Assume you are working on a Neural Network project which requires you to solve a few hundred problems or you might need to solve the same problem multiple times using multiple ways to figure out which one is the most efficient. For such tasks you need to choose a language in which you can turn your ideas into code , see results and make changes if you need as fast as you can. The more you are faster in this testing step , the more productive you will be. This is the reason why programmers don't want to mess with Low level stuffs here.

Think what will be the impacts (both advantages & disadvantages) of using C/C++ for such projects.

Advantages:-

  • Performance will be grate if code is well optimized.
  • You can manage memory however you want.
  • You will have control over every single line of low level code base.
  • Data processing rate will be much faster.

Disadvantages:-

  • Low level stuffs like Memory Management , Data Type , Pointer will require a great attention and huge time which is a red mark for faster development.
  • Events like Exception handling will be a headache in this case , cause C/C++ is unmanaged language.
  • You might need to re-write some parts or the whole of some useful libraries or frameworks for C/C++.
  • You need to mess with things like Memory Leak / Overflow , Garbage Management.
  • You will need more time & development cost might rise.
  • Maintaining your codebase will be a nightmare.

Optimizations such as performance or others can be done at Deployment Stage. But programmers usually don't bother with those at Development Stage. They basically chose languages keeping two primary keys in mind , they are:-

  • How much productive the language is.
  • Whether plenty of useful libraries / frameworks are available to use with that language or not.

Python satisfies those conditions well. Considering those , other language ecosystems are not even closer to Python.

But admit it or not , truth is

A programmer spends 90% of his time in Python while writing code for AI. On the other hand 99% of CPU time is spent on C/C++. Cause Python , its interpreter & most of the used Libraries are just abstraction over C/C++ under the hood.

Top comments (14)

Collapse
 
intrinsicworlds profile image
IntrinsicWorlds

While I essentially agree with your position, the main point is somewhat of a false dichotomy.

Under C++ or C, you have the machine languages that they compile down to as well, that argument would be the same as why isn't everyone still programming in Assembly?

The answer is as you almost clearly stated, ease of access. Assembly is not at all easy to write or think about in high cognitive ways easily.

Thus came languages to make that easier.

Then came more, and more and more. Each abstracting more of the banalities of the previous ladder step away.

Pythons main success is three fold;

  1. It is extremely easy to learn, it has a very low barrier to entry. This allows people who are more and more forced towards programming, such as researchers, to pick up scripting or simple notebook style programs easily.

This allows for quick to market, quick to prototype, instant exploration of idea to code to output. It is extremely powerful to think, write a few lines of something, and begin to see what that looks like.

  1. It has very deep wells of power. While many in the traditional language world may have, or still, considered python "just a scripting" language it is far from the truth. It is an extremely deep and powerfully full bodied language. You can go from simple scripts and running a simple data set in a few minutes to enterprise level software applications. It spans technologies, use cases and industries with ease and has extreme reach into numerous technological arenas.

  2. It is tightly coupled, though this is true of CPython, which is the normal implementation people are aware only one of many implementations, with C and C++. This allows interop with tons of existing, and new libraries to glue, or, place performance code into lower level code where needed allowing python to straddle both fast to market and performance operation.

This is extremely difficult for almost any other language in the way that python has done it. The success and eponymous nature of it for data sciences is only because it is an every tool that is easy to start, and powerful enough to carry you through enterprise level work.

Collapse
 
caroso1222 profile image
Carlos Roso

Great analysis. I think Python has also helped Machine Learning to rise in popularity these last years. Like, it's first too academic, then it jumps to low level software, and then it opens up to the public when it lands to such glue languages. Nice post!

Collapse
 
evilprince2009 profile image
Ibne Nahian

Personally i'm not a huge fan of Python. Rather Java, C++, C# feels more native to me. But I have no way but admit that Python helped growing AI , ML , DS things more than anything else.

Collapse
 
vharihar2 profile image
Skeynes Pronty • Edited

Indentation. I hate the "Indentation defines scope" paradigm of Python! I'd rather have the IDE beautify/indent the code rather than enforce it by having it imply scoping.

Collapse
 
evilprince2009 profile image
Ibne Nahian

Agree 😂😂
Curly braces are better than shitty indentations.

Collapse
 
misterscott profile image
MisterScott

As just a brainstorm, I wonder about a translation layer plugin for editors (and error reporting going back) that would indent guided by curly brackets.

Collapse
 
ebinsaad profile image
ebinsaad • Edited

I would say python is enough for exploring ideas and staging. Most of the time will do just good in production. But many teams will end up porting python data science code to C/C++ or even write from scratch when python hits it is performance limits.

Productivity is not an issue for expert C/C++ developers. The challenge is to find those experts C/C++ developers.

Collapse
 
atldev profile image
Chris

I don't argue with your point that Python has less pitfalls and is more productive for higher level programming, and that C++ is more performant, but it is erroneous to imply there is any "garbage" memory to be collected in C++; there is no Garbage Collector. This provides the precise control over memory usage (and performance) that make C++ the systems language of choice, despite it's complexity. I am interested in Rust though. Rust provides compiled performance with automatic but synchronous memory deallocation for more deterministic performance characteristics and memory profiles when compared to asynchronous GC approaches like those applied in Java, .Net, and GoLang.

Collapse
 
neokeats profile image
neokeats

« synchronous memory deallocation for more deterministic performance characteristics and memory profiles when compared to asynchronous GC approaches like those applied in Java, .Net, and GoLang. »

It’s not that obvious.
GC can be more performant because it’s not up to the dev to decide when the desallocation should happen.
It’s the role of the GC to decide if the system don’t have better tasks to do and deallocate memory at the right time by doing it for all unreferenced data in one go.
Of course in theory you can be as efficient about as a program but in the context of others program run at the time as yours, it’s less than obvious. You’ll most likely slow the machine by running memory desallocation while there is largely enough memory left etc...

Collapse
 
evilprince2009 profile image
Ibne Nahian • Edited

Im not criticising C++. Its one of the best programming languages ever made. But Im not talking about system programming or OS, Kernel stuff. System programming is something that requires more efficiency rather than productivity. AI, ML, DS things are completely different. If you deal AI, ML things with a VM language like Python or JS , thats fine. But when it comes to system programming , VM languages are horrible choice. Despite of all complexities , drawbacks C/C++ are by far the best choice for system programming.

Collapse
 
misterscott profile image
MisterScott

Just mentioning that there are GC libraries available for C/C++. You are not forced to use them.

Collapse
 
ykalatzis profile image
Yannis Kalatzis

Clarifying. Where is Julia in this landscape?

Collapse
 
lm8 profile image
lm8

There are several AI projects that are C/C++ based and don't necessarily require Python to use them: lmemsm.dreamwidth.org/16168.html

Collapse
 
evilprince2009 profile image
Ibne Nahian

Let me know your opinion on this topic.