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).
# 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]
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''
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.
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