One of if not the most popular programming language for data science and AI today is Python, with R being a frequently cited runner-up. However, both of them are interpreted languages, which do not execute as fast as compiled languages. Why is that the case? The main advantage of AI over humans is in computing speed, and with real world AI applications today handling big data, execution time will already increase considerably as it is. Why aren't compiled languages preferred for this very reason?

Sure, the key argument and strength going for Python is its vast range of third party libraries available for AI, like scikit etc. but such communities can take root and grow anywhere under the right circumstances. Why did this community end up growing around Python and not a faster, equally common compiled language like C++, C# or Java?


There are a few advantages of interpreted languages (compared to compiled languages)

  • platform independence (you only need the interpreter for your platform, even though this is not true if e.g. your interpreted language is only a wrapper library around a library written in another programming language)
  • dynamic typing (no need to specify the types of the variables)
  • dynamic scoping (e.g. you can access variables in other scopes)
  • automatic memory management (but there are compiled languages, like Java, that also have a garbage collector)
  • Rapid prototyping (for various reasons, including dynamic typing), hence software can be written more quickly

Hence, the main advantages of an interpreted language compared to a compiled language are flexibility and dynamism. Given that AI is still an evolving field, these characteristics are widely appreciated.

There is also at least one disadvantage of interpreted languages (compared to compiled languages)

  • Slower running times compared to compiled languages, which, once compiled, are quite fast, because they are often compiled to a code that is quickly executable by the machine or virtual machine

Python and R are widely used in data science and artificial intelligence because of the advantages above, which possibly contributed to the rapid growth of the communities around them and the development of software libraries.

However, note that the core of the most common machine learning libraries today, including TensorFlow and PyTorch, is written in a compiled language like C and C++. In the specific case of TensorFlow, Python is just a wrapper library. Consequently, under the hood, the code is not executed by the Python interpreter, but first compiled, which implies that, when you're using e.g. TensorFlow, your code will run (more or less) as fast as if you were using a compiled language like C. A similar argument can be made for libraries like NumPy, where Python is just a wrapper library.

| improve this answer | |
  • $\begingroup$ is there any good reason why python may not be compiled to be as fast as C? $\endgroup$ – user8426627 Jun 22 '19 at 17:56
  • $\begingroup$ @user8426627 Python code can be first compiled to an intermediate code (called bytecode). See e.g. stackoverflow.com/q/471191/3924118. So, Python is both a compiled and interpreted language (like Java). There's also a version of Python that is only compiled Cython, which should give you C-like performance, but this is not the official distribution of Python. If you decide that you want a compiled language, then the language might lose flexibility in certain aspects, because you want to optimize towards the specific architecture (or OS). $\endgroup$ – nbro Jun 22 '19 at 18:52

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