Home  >  Article  >  Backend Development  >  Why does C code run faster than Python?

Why does C code run faster than Python?

PHPz
PHPzforward
2023-09-11 12:45:02946browse

Why does C code run faster than Python?

In this article, we will learn why C code runs faster than Python.

Guido Van Rossum developed Python, one of the most famous programming languages. Python is popular among developers for its clear syntax and simple code, even for novices. Learning Python is highly beneficial for those who are just starting their programming career. They can use Python programming training, blogs, videos, modules, and thousands of other resources to learn all aspects of this popular language. Upon completion, you will be able to perform modern development activities such as GUI development, web design, system administration, complex financial transactions or calculations, data science, visualization, and more.

Python is slower than C because it is an interpreted language.

Python is slower than C because it is an interpreted language.

Thus, more real CPU instructions are required to execute a given statement.

You can verify whether the value of a variable is less than, greater than, or exactly equal to that value by adding the number 1 or comparing it to a given value in your Python code.

The difference is that the Python code is not immediately executed by the CPU, but is interpreted.

In terms of performance, this makes a world of difference.

Almost always use a virtual machine to run Python code

Another name for a virtual computer is "bytecode interpreter".

Interpreted code is always slower than actual machine code because it requires more instructions to implement the instructions than to execute the actual machine instructions.

Example

Consider the expression x = 1. On Intel CPUs, a register increment is a single operation with a latency of 1 and a throughput inverse of one-third (1/3).

In other words, it refers to the fastest CPU instructions that Intel processors can provide.

In Python, how is x = 1 implemented?

To understand this, you must first understand how Python works internally.

Python’s internal components include tokenizer, lexical analyzer, bytecode generator and bytecode interpreter -

  • Tokenizer - It creates a stream of tokens from a given ASCII text file (Python code).

  • Lexical Analyzer - This area of ​​Python involves proper indentation and spacing. A syntax check is performed at this point.

  • Bytecode Generator - If any optimizations are done, they are done by the Python component; however, since Python is not a compiled language, compared to the C compiler available The scope of optimization is limited.

  • The Python module known as the "bytecode interpreter" manages the bytecode stream and powers the Python virtual machine (maintains its state).

Once generated, bytecode is usually cached in memory.

This improves speed because you don't have to repeat the tokenization, lexical analysis, and bytecode generation process for code that Python has already seen.

So instead of going through the tokenization, lexing, and bytecode creation process every time we loop through the while loop, we can continue to pass the bytecode to the bytecode interpreter.

Isn’t this faster? No, that's not actually the case.

Although using cached bytecode is faster, it does not execute or operate as quickly as machine code.

The real CPU running the code is not a virtual computer.

Compilation process

Compiled UCSD Pascal, unlike other compiled languages ​​at the time, was not compiled into assembly language. Instead, it is compiled into p-code.

So when you think of "compiled Pascal programs" you think of p-Code. If you like Java or Python and want to pretend you've come up with something new, use "bytecode".

In addition, Python also includes the concept of "compiled Python", which refers to Python that has been processed by a tokenizer, lexer, and bytecode generator to create cached bytecode that can be provided to the bytecode interpreter. Python code (aka Python virtual machine).

When you see a file with a .py extension, it is an ASCII text file containing Python source code.

PYthon, compiled is what a file with a ".pyc" extension represents.

Still, the virtual computer executes the created code.

Native code

Once a program is built, it is not fully converted to native code until it is converted to the native binary CPU instructions of the platform for which it was designed.

This usually involves writing assembly code, passing it to the assembler, and then letting the assembler create platform-specific object files instead of using bytecode.

Until the program is connected to the platform runtime, it is not ready for use. The runtime can provide runtime services such as dynamic object loading and build an environment for code execution. In compiled C, there is a runtime. Compiled C has a runtime.

Why is Python slower than C?

  • Python performs extensive sanity checks - integers never overflow, invalid memory is never accessed, types are never (silently) incorrect, and arrays are never written beyond their ends or read. In Python, it's rare to get a "non-local error", but in C, it's fairly common to get errors that aren't actually reported errors.

  • Python's compiler doesn't do very advanced optimizations (if any) - for one thing, speed isn't as important as in C, and there's not as much information to go on - for example, in Common Lisp (another a dynamic language like Python) where you can provide type annotations to get the same speed as C - if you opt out of the safety checks and promise that certain variables will have certain types, you get the exact same machine code instructions (The exact same strange behavior will occur if you have a bug in your program).

in conclusion

In this article, we looked at the different reasons why C code executes faster than Python.

The above is the detailed content of Why does C code run faster than Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete