“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:
- divmod’s nevow
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.
- Introduction to Python3
- Itertools in Python3
- New '=' Operator in Python3.8 f-string
- Positional-only Parameter in Python3.8
- How to print spaces in Python3?
- Python3 for GUI application | An Overview
- How to implement Dictionary with Python3?
- How to write Comments in Python3?
- Python3 Intermediate Level Topics
- Automate the Conversion from Python2 to Python3
- Different Input and Output Techniques in Python3
- Convert String to Double in Python3
- Python3 - if , if..else, Nested if, if-elif statements
- Setting python3 as Default in Linux
- Python2 vs Python3 | Syntax and performance Comparison
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.