PyPy – Introduction

What is PyPy : 

PyPy is a very compliant Python interpreter, almost a drop-in replacement for CPython 2.7.10 and 3.3.5. It’s fast due to its integrated tracing JIT compiler. 

On average, PyPy is 4.2 times faster than CPython. Sometimes Cpython may get TLE  while execution but the same code may run fine with PyPy.In general if c++ takes 1 second to execute a file , if we execute same file with python it will take upto 5 seconds.

PyPy

Advantages and distinct Features

  • Speed: thanks to its Just-in-Time compiler, Python programs often run faster on PyPy.

  • Memory usage: memory-hungry Python programs (several hundreds of MBs or more) might end up taking less space than they do in CPython.

  • Compatibility: PyPy is highly compatible with existing python code. It supports cffi, cppyy, and can run popular python libraries like twisted, and django. It can also run NumPy, Scikit-learn and more via a c-extension compatibility layer.

  • Stackless: It comes by default with support for stackless mode, providing micro-threads for massive concurrency.

Example #1:

#python Program
import timeit

 

start = timeit.default_timer()

 

#Your statements here
for i in range(10000):
    for j in range(1000):
        if(i==0 and j==0):
            print(‘In the loop’)
 
print(‘Out of loop’)
stop = timeit.default_timer()

 

print(‘Time: ‘, stop – start)

Output :

In the loop
Out of loop
Time: 0.91280354000628
#pypy Program
import timeit

start = timeit.default_timer()

#Your statements here
for i in range(10000):
    for j in range(1000):
        if(i==0 and j==0):
            print(‘In the loop’)
        
print(‘Out of loop’)
stop = timeit.default_timer()

print(‘Time: ‘, stop – start) 

Output :

In the loop
Out of loop
Time: 0.02118576504290104