Why is Python slower than other languages?

Science & TechnologyComputersEducationPython

Python is a scripting language while C is a programming language. C/C++ is relatively fast as compared to Python because when you run the Python script, its interpreter will interpret the script line by line and generate output but in C, the compiler will first compile it and generate an output which is optimized with respect to the hardware. In case of other languages such as Java and.NET, Java bytecode, and .NET bytecode respectively run faster than Python because a JIT compiler compiles bytecode to native code at runtime. CPython cannot have a JIT compiler because the dynamic nature of Python makes it difficult to write one.

The Difference

As we know, Python is an interpreted language, while C is a compiled language. Interpreted code is always slower than direct machine code because it takes a lot more instructions in order to implement an interpreted instruction than to implement an actual machine instruction. Before actual work could be done by the program, Python instructions should be understood by the CPU. So the Python interpreter checks each statement according to the rules of the Python language such as allocation of memory for storage of the variables, straining out blank spaces and comments from the program and other related tasks. This process is repeated for each line of the program and adds significantly to the overhead of program execution.

C Is Quick

On the other hand, C does not spend much time on re-examining the source code and quickly converts into CPU commands. Before execution, a separate compiler converts human language program into CPU instructions, checks for errors, allocates memory and variables, stripes out comments and blanks, and optimizes the resulting instructions. The result of the compiled code is linked into other pre-made codes and as a result, you will get solid CPU commands, which are ready to perform the assigned job, without much preparation. Internally, the reason for Python code executing more slowly is that the code is interpreted at runtime instead of being compiled to a native code at compiling time.

raja
Published on 04-Jan-2019 15:17:31
Advertisements