The different ways Python, Java, C++, and C# execute code

jaycob okoro
4 min readJun 3, 2019

--

Source: Alamy stock photos

Most computer languages are high-level, human-readable languages which cannot be comprehended by the machine. Taking this into consideration, the only way for these languages to be understood by a machine is through some form of translation.

Broadly speaking, there are three ways of carrying out this translation.

1· The first involves compilation into machine code. (C++).

2· The second is the direct execution of source code by an interpreter(Python, PHP, and other scripting languages).

3· Finally, there is the compilation of the source code into an intermediate byte-code which is then interpreted by a run-time system(Java).

While these come with different advantages and trade-offs, the succeeding text shall discuss each language in terms of how they execute instructions at run time.

Compilation of C++ codes:

Photo by Capt-Jc on Deviantart

A compiler translates source code into machine code in four steps:

1) Pre-processing- The C++ pre-processor copies and generates a macro-code from the source code file.

2) Compilation- where the expanded source code file is converted into an assembly language by the compiler.

3) Assembly- where an object code is then generated from this assembly code for the platform.

4) Linking- where these object codes are bundled up into an executable file by a program called a linker.

While this process enables fast execution speeds, recompilation is required every time the source code is altered or ran on a machine with a different OS.

Interpretation process in python:

Source: knowledgehi.com

Rather than convert the source code into machine code, an interpreter directly executes instructions written in the source code. In this way, the interpreter serves as a virtual machine, while the source-code serves as machine language for the interpreter.

This impacts execution speed because the interpreter has a slower runtime than the actual machine.

However, no recompilation is needed for every change to the source code. In addition, it allows the program to run on any operating system.

Java and the intermediate code.

Source redbubble.com

This is sort of a hybrid between compilation and interpretation, and thus presents advantages unseen in the previous two; i.e, in addition to its relatively fast execution, it has cross-platform capabilities.

How it works:

The platform dependent issues of C++ are abstracted away by the virtual machine (JVM). This means the java compiler doesn’t create an object code but a byte-code which is then executed by the JVM (Java interpreter), in a more efficient manner than other interpreters.

In essence, the Java runtime environment (JRE) which includes the JVM is needed to run the Java program while the Java development kit (JDK) is needed for compilation as it contains the Java compiler. Bearing in mind that JREs have been created for most devices, (including PCs, laptops, mobile phones, and internet devices, we now know why Java is highly portable and fast.

However, due to a series of run time checks carried out by the program to ensure proper memory allocation, and prevent the system crash, Java is quite a bit slower than the compiled language (C++ doesn’t have this).

C# and its special compilation

Source: eduonix.com

It bears similarities to both Java and C++. With respect to Java, its compiler creates byte-codes instead of java codes; but like C++, its byte codes are directly linked with the necessary .dll files and executed directly on the machine, not a virtual environment. This, in essence, means that, like C++ programs, they are not cross-platform, and are recompiled for different devices and changes to the source code.

However, Microsoft published an open source engine called virtual execution system, with definitions for c# and a common language infrastructure.[3]

Final notes:

Photo by Lee Campbell on Unsplash

In hope that this article has clarified certain concepts, I’d like to add that the speed of execution should not be a major determiner in your language choice; cognizance should also be given to, its reliability, market demand, community support, machine constraints, available libraries and most importantly the intended platform of use(web, mobile or desktop), before making the choice.

Sources:

[1] How Java Works — Jeffery S. Carroll

[2] The C++ compilation process — Kurt McMahon

[3] Object orientated programming using c#- Simon Kendall

[4] A qualitative study of major programming languages — Shafi Sheikh, Ghazala & Islam, Noman. (2016)

[5] What is the difference between a compiled and an interpreted program?-Knowledge base, Indiana University.

[6] Pointers and Memory — Nick Parlante

--

--