In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on how PyPy makes Python code run as fast as C. The article is clear and organized, very suitable for beginners to learn, and worth reading. Interested friends can read along with Xiaobian. I hope everyone gets something out of this article!
1. introduction
As an algorithmic engineer, how to quickly implement an idea and verify whether it works is crucial to your daily work. Python is an excellent tool for doing this easily. It allows us to focus on the idea itself without getting bogged down by the code implementation.
However, as you've probably heard, Python has a fatal flaw: it runs much slower than compiled languages like C or C++. So, after we've implemented an idea quickly by building Python, and now we want to turn it into a fast and high-performance tool, what do we do? Usually, we end up spending about twice as much time manually converting Python code to C/C++.
But wouldn't it be nice if our Python code itself could run faster? So how?
Fortunately, I stumbled upon a solution to this problem: PyPy, which is a quick alternative to Python runtime.
2. give a chestnut
To see how much speedup PyPy can improve, I ran both the default Python interpreter and PyPy in the following example
The code is as follows:
import timefrom termcolor import coloredstart = time.time()number = 0for i in range(100000000): number += i print(colored("FINISHED", "green"))print(f"Ellapsed time: {time.time() - start} s")
In simple terms, the script above adds up all integers between 0 and 100,000,000 in a loop and prints a message and the entire code script runtime when it is complete.
The comparison results are as follows:
Despite the simple contrast, the acceleration effect of the above example is still exciting. PyPy executes in just 0.22 seconds compared to the default Python interpreter, which takes about 10 seconds! Also, note that we can feed Python code directly to PyPy without making any changes to the code.
The results are even more impressive when we compare them to the C implementation. On my computer, the equivalent implementation in C takes 0.32 seconds. Although C remains a master of speed overall in most cases, PyPy can beat C in some cases.
Note:
PyPy is less efficient when most of our program's runtime comes from calling non-python libraries (such as Cpython). However, if we have a slow program that spends most of its time executing code that calls Python libraries, PyPy can greatly improve the efficiency of the code.
3. root-finding
If this is your first encounter with PyPy, you may be asking yourself,"What's behind how PyPy works so fast? "
Uh... Looking back at our experiments, we ran exactly the same code, and using PyPy seemed to get huge speedups for free, black tech hey.
Although the code is identical, the execution of the code in both ways is quite different. The secret to PyPy's performance improvement lies in just-in-time compilation, or JIT compilation.
3.1 compiled in advance
C, C++, Swift, Haskell, Rust and other programming languages are compiled in advance. This means that after we have written some code in these languages, we need to click a build button and the compiler will convert the source code into machine-readable code that can be read by a particular computer architecture. Every time you execute a program, your original source code is long gone. It's just machine code.
3.2 interpretability of language
Python, JavaScript, PHP and similar development languages take different approaches. They can all be explained. Source code remains unchanged compared to converting source code to machine code. Every time the program runs, the interpreter "looks" at the code line by line and runs it for us.
For JavaScript, every Web browser has an interpreter built in. The standard Python interpreter is called CPython. However, it is important to distinguish Python scripts from interpreter tools that run code, because we can have completely different tools that all have the ability to run Python code. This is where PyPy comes into play.
3.3 JIT
PyPy is an alternative implementation of Python that takes advantage of just-in-time compilation. The principle behind PyPy is that PyPy starts out like an interpreter, running our Python code directly from source files. But instead of running code line by line, PyPy compiles parts of code into machine code before executing them, so to speak, in time.
In this sense, JIT compilation is a combination of interpretation and early compilation. Not only do we get the performance boost of early compilation, but the flexibility and cross-platform usability of interpreted languages are preserved.
Thank you for reading, I believe you have a certain understanding of "PyPy is how to make Python code run as fast as C", go to practice it quickly, if you want to learn more about related knowledge points, you can pay attention to the website! The editor will continue to bring better articles to everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.