Why PyPy3 is preferred over Python3?
Last Updated : 05 Oct, 2021
"If you want your code to run faster, you should probably just use PyPy."
-- Guido van Rossum (creator of Python)
If you have ever coded in Python, you know how much slower it is compared to some of the other popular programming languages. In most of the online code judges, the time limit of Python is as much a 5 times more than that of C and more than twice as that of Java.
The reason why Python usually takes 10 to 100 times more in execution is that it is a higher-level language that is dynamically typed. No matter how optimized your code is, it can't outdo C/C++ in execution time as is. However, Python is a fun language and easy language to work with, which is why programs are built much faster with it.
To solve this problem, let's understand what Python is
Python is not a single language but rather it's a way of implementing python code. The default and the most widely used Python implementation is CPython. The flow of execution of code in CPython is :
- The interpreter checks for logic and syntax errors
- After finding no errors, the formatted code is converted into Byte Code
- The Byte Code is sent to PVM(Python Virtual Machine) which converts the code into machine-readable language on which CPU performs operations.
However, CPython is not the way to implement Python. In fact, there are many other implementations:
- IronPython (Python running on .NET)
- Jython (Python running on the Java Virtual Machine)
- PyPy (A fast python implementation with a JIT compiler)
- Stackless Python (Branch of CPython supporting micro threads)
- MicroPython (Python running on microcontrollers)
PyPy is built using the RPython language that was co-developed with it. RPython (Restricted Python) is a subset of Python language which puts some restrictions on the Python language to make it run faster. The main reason to use it instead of CPython is its speed. Specifically, it usually runs 4.4 times faster than CPython. PyPy implements Python 2.7.13 and 3.6.9. It supports all of the core languages, passing the Python 2.7 test suite and most of the 3.6 test suite (with minor modifications) It supports most of the commonly used Python standard library modules. This means that in most cases your python code will run without any need of modifications.
PyPy uses a technique known as meta-tracing, which transforms an interpreter into a tracing JIT (just-in-time) compiler which is a way of executing code that involves compilations during runtime. It not only runs faster but it also has better memory usage than Python. It is also highly compatible with some of the most used libraries that can be used in Python.
Some of which are:
- ctypes
- django
- sqlalchemy
- flask
- twisted
- pylons
- divmod's nevow
- pyglet
- Pillow
- lxml
- NumPy
With so many upsides it is bound to have some negatives as well.
Disadvantages of PyPy
PyPy cannot execute all of the Python code. Some modifications may be necessary to the Python code to execute. The external C-API have been reimplemented in PyPy but sometimes some C-abstractions leak out on CPython and are abused, perhaps even unknowingly. It requires a "Warm-up" time which causes a slight to noticeable delay in the initial execution of an application, due to the time taken to load and compile the bytecode. Smaller the execution worse will be its performance.
Similar Reads
Why is Python So Popular? One question always comes into people's minds Why Python is so popular? As we know Python, the high-level, versatile programming language, has witnessed an unprecedented surge in popularity over the years. From web development to data science and artificial intelligence, Python has become the go-to
7 min read
Why is Numpy faster in Python? NumPy is a Python fundamental package used for efficient manipulations and operations on High-level mathematical functions, Multi-dimensional arrays, Linear algebra, Fourier Transformations, Random Number Capabilities, etc. It provides tools for integrating C, C++, and Fortran code in Python. NumPy
4 min read
Python2 vs Python3 | Syntax and performance Comparison Python 2.x has been the most popular version for over a decade and a half. But now more and more people are switching to Python 3.x. Python3 is a lot better than Python2 and comes with many additional features. Also, Python 2.x is becoming obsolete this year. So, it is now recommended to start using
5 min read
Why Python is a High Level Language Python is categorized as a high-level programming language because of several key characteristics and features that distinguish it from lower-level languages ââsuch as assembly language or machine code. In this article, we will see why Python is a high-level language. What Does High-Level Language M
5 min read
History of Python Python is a widely used general-purpose, high-level programming language. It was initially designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It was mainly developed to emphasize code readability, and its syntax allows programmers to express concepts in fewer lines of
5 min read