Why does Python run slower than C?

Photo by Nick Abrams on Unsplash

Why does Python run slower than C?

The simplest answer to this question is:

python is interpreted so it's slower.

Thus causing a delay in execution. Most of the time this doesn't matter, the ability to quickly throw together a prototype is more important than raw grunt (i.e. developer time is usually much more expensive than processor time).

As mentioned, Python is an interpreted language, which actually means that the source code of a Python program is converted into bytecode that is then executed by the Python virtual machine.

Although, Python is different from major compiled languages, such as C and C++, as Python code is not required to be built and linked like code for these languages. This distinction makes for two important points:

Python code is fast to develop: As the code is not needed to be compiled and built, Python code can be readily changed and executed. This makes for a fast development cycle. Python code is not as fast in execution: Since the code is not directly compiled and executed and an additional layer of the Python virtual machine is responsible for the execution, so Python code runs a little slow as compared to conventional languages ​​​​​​​​​​like C. Now to have a clear visualization of the above process, let's discuss the execution process of python code .

Python programing language is an interpreted language that converts the python code to bytecode. These bytecodes are created by a compiler present inside the interpreter. The interpreter first compiles the python code to the byte code which is also called the intermediate code, then the code is used to run on the virtual machine. In the virtual machine, the library modules of the python get added, and then the code is ready to run on a machine.

Steps for interpretation of python source code: Source code: Python Code Compiler: Enters inside the compiler to generate the bytecode Bytecode: Intermediate code or low-level code Virtual Machine: Here the code gets support from the library modules. Python is slower as compared to other programming language, but the process of converting the python code to the bytecode makes it faster to access each time after the code is interpreted once. This bytecode is saved in the file named the same as the source file but with a different extension named "pyc".