The myth of the fastest programming language

Prajwal Krishnan / June 20, 2022

5 min read

success-jump

When talking about programming languages, people often mention speed. You always hear about performance comparisons across multiple languages; which is the fastest programming language; Python is slower than Java, and so on. The only problem with these statements is that they actually make no sense. Let me explain why.

What Is a Programming Language, Really?#

In order to understand why comparing the runtime performance of various programming languages is silly, we need to clarify what a programming language actually is.

Just like any other type of language, a programming language is a tool used to communicate a message, in this case, its purpose is for humans to give instructions to computers for execution.

Now, you would agree that a spoken language like English --- with its grammar, syntax, and vocabulary --- is an abstract concept, isn't it? You cannot touch English, nor you can smell or taste it. However, you could argue that you can see and read English text, or you can indeed hear English words. Does this make a language concrete?

Actually, what you read and hear is an implementation of English. You use the abstract language rules to embed a message into a concrete medium, be it sound waves or ink on a piece of paper.

Now that we have clarified the difference between languages and their implementations, let's go back to programming languages and apply these concepts. Just as you wouldn't say that English is faster than Italian, claiming that C is faster than Python is nonsense.

What can be compared instead are the language implementations. A person speaking can usually communicate the same message faster than writing. The same holds true for programming languages: directly executing a binary is faster than interpreting its high-level code equivalent.

How Programming Languages Are Implemented#

Since we have brought up the concept of natural language implementations as an analogy, it's now time to talk about the concrete implementation of programming languages: meet compilers and interpreters.

As you probably already know, your high-level source code has to be translated into machine code before the CPU can execute it. This translation is achieved by applying the language rules to reshape the message while still, ideally, maintaining the same meaning, just as you would do when translating from English into Italian.

The language speed misconception originates right here, when people mistake the default implementation with the language itself. You often hear that Python is interpreted, C is compiled, Java is something in between, and so on. However, that's the same as claiming that English is a spoken language by nature, excluding its written form, or even English thoughts or electric signals carrying English messages.

As regards programming languages, it's true that Python is usually implemented through an interpreter: CPython, while Java is normally compiled into byte code by javac and then interpreted by the Java Virtual Machine (JVM). These are all ways of communicating instructions from the developer to the computer.

Different Language Implementations#

Now, one medium can indeed be more performant than others. However, languages are not bound to any single medium of communication, just like English isn't exclusively spoken.

In fact, you can develop a Python compiler or a C++ source code interpreter (which already exists, btw.) and use a different implementation. Would that make C++ slower than Python? Absolutely not, that would just mean that one implementation is more efficient than another.

Moreover, your code highly influences performance, regardless of the underlying low-level implementation. For example, let's assume that JavaScript's console.log() is able to print a string to the console faster than C's printf() function. That might tell us that what is really inefficient is that specific function, not the interpreter/compiler itself.

This is also why performance benchmarks of code snippets with the same meaning may be meaningless. To demonstrate this, let's take a look at these different ways of iterating over a list in Python:

And the benchmark results:

Foreach loop: 0.012268301301810424 seconds
While loop: 0.0842185921021155 seconds
For range loop: 0.044092743896180765 seconds
List comprehension: 0.01865486839815276 seconds
The fastest method is Foreach loop with 0.012268301301810424 seconds

As you can clearly see, if you were to perform a reliable benchmark between different programming languages, you should also take into account the fact that there are more and less efficient approaches to achieve the same result. Because of this, performance comparisons across different programming languages are likely to be biased and may not be so meaningful.

Beyond these aspects, another crucial and non-negligible factor of performance is the developer's skill to write efficient code.

Conclusion#

To wrap it up, we have clarified the difference between languages and their implementation. Then we talked specifically about programming language implementations and how they can affect the execution speed of code. Lastly, we discussed how code snippets that achieve the same goal can perform significantly better than others, which should be a cause of concern when benchmarking.

The myth of the fastest programming language is just a common misconception that really makes no sense if you think about it. I think it's time to eradicate this false belief and bring clarity, especially to beginner programmers who are more likely to keep this myth alive.


2021 Prajwal Krishnan