Thursday, December 26, 2024
Google search engine
HomeLanguagesWhy PyPy3 is preferred over Python3?

Why PyPy3 is preferred over Python3?

“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 :  

  1. The interpreter checks for logic and syntax errors
  2. After finding no errors, the formatted code is converted into Byte Code
  3. 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. 

 

RELATED ARTICLES

Most Popular

Recent Comments