Speed comparison between CPython and PyPy

Comparison of processing speed to add elements to the list

Premise

As a comparison of processing speed, we compared the execution time of CPython, which is the official implementation of Python, and PyPy, which is the JIT compiler implementation by Python of Python.

For the following three functions used in Python list comprehension, How to measure execution time with Python Part 1 I used the decorator defined in (: //qiita.com/intermezzo-fr/items/9ac2916a9155d5317ebc).

Function to be evaluated

# 1. testfunc1:Prepare an empty list and append
@time
def testfunc1(rangelist):
    templist = []
    for temp in rangelist:
        templist.append(temp)

# 2. testfunc2: 1+Objectify append
@time
def testfunc2(rangelist):
    templist = []
    append = templist.append
    for temp in rangelist:
        append(temp)

# 3. testfunc3:List comprehension
@time
def testfunc3(rangelist):
    templist = [temp for temp in rangelist]

Decorator for time measurement

def time(func):
    import functools
    import datetime
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        start = datetime.datetime.today()
        result = func(*args, **kwargs)
        end = datetime.datetime.today()
        return end - start
    return wrapper

The versions of CPython and PyPy are:

CPython

Python 2.7.4 (default, Apr  6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

PyPy

Python 2.7.3 (87aa9de10f9c, Nov 24 2013, 17:46:53)
[PyPy 2.2.1 with MSC :.1500 32 bit] on win32
Type "help", "copyright", "credits" or "license" for more information
And now for something completely different: ``PyPy 1.3 released''

result

For each function, the process of adding 10,000,000 elements to the list was performed 10 times, and the average execution time was obtained.

CPython

>>> rangelist = range(1,10000000)
>>> print reduce(lambda x, y: x + y, [testfunc1(rangelist) for temp in range(0,10)])/10
0:00:00.998600
>>> print reduce(lambda x, y: x + y, [testfunc2(rangelist) for temp in range(0,10)])/10
0:00:00.723500
>>> print reduce(lambda x, y: x + y, [testfunc3(rangelist) for temp in range(0,10)])/10
0:00:00.399900

PyPy

>>> rangelist = range(1,10000000)
>>> print reduce(lambda x, y: x + y, [testfunc1(rangelist) for temp in range(0,10)])/10
0:00:00.290300
>>> print reduce(lambda x, y: x + y, [testfunc2(rangelist) for temp in range(0,10)])/10
0:00:00.275500
>>> print reduce(lambda x, y: x + y, [testfunc3(rangelist) for temp in range(0,10)])/10
0:00:00.046300

For an exact comparison, you'll need to skip the first one and do a lot, but we're not doing anything here.

Conclusion

Each magnification (CPython execution time รท PyPy execution time) is as follows. The higher the number, the faster PyPy.

In PyPy, testfunc3, the list comprehension version, is especially fast. I was thinking of studying Lua related to JIT compiler, but I thought that I should study PyPy in this case.

Recommended Posts

Speed comparison between CPython and PyPy
Speed comparison between CPython and PyPy
File write speed comparison experiment between python 2.7.9 and pypy 2.5.0
Speed comparison between incrementing count variable and enumerate
Difference between MicroPython and CPython
File open function in Python3 (difference between open and codecs.open and speed comparison)
speed difference between wsgi, Bottle and Flask
Speed comparison of murmurhash3, md5 and sha1
Create new file [Comparison between Bash and PowerShell]
Between parametric and nonparametric
[Ruby vs Python] Benchmark comparison between Rails and Flask
Image capture / OpenCV speed comparison with and without GPU
CPython vs PyPy vs Pyston
[Ubuntu] [Python] Face detection comparison between dlib and OpenCV
[Python] How to set variable names dynamically and speed comparison
BASIC and C and assembler speed comparison and optimization play with IchigoJam
AtCoder ABC151 Problem D Speed comparison in C ++ / Python / PyPy
Performance comparison between 2D matrix calculation and for with numpy
Difference between process and job
Correspondence between pandas and SQL
Conversion between unixtime and datetime
Python, Java, C ++ speed comparison
Difference between regression and classification
Collaboration between PTVS and Anaconda
Difference between np.array and np.arange
Python 3 sorted and comparison functions
Cooperation between py2exe and setuptools
Comparison of Apex and Lamvery
Boundary between C and Golang
Difference between ps a and ps -a
Difference between return and print-Python
Speed comparison of Wiktionary full text processing with F # and Python
Story of speed comparison of sort of numerical value and character string (unfinished))
Search for character strings in files [Comparison between Bash and PowerShell]
Summary of differences between Python and PHP (comparison table of main items)