[BangPypers] Would pypy result in a recast of what are the more efficient pythonic idioms ?

Dhananjay Nene dhananjay.nene at gmail.com
Thu Aug 11 12:17:50 CEST 2011


I had done some benchmarking of code in various languages 3 years ago. I had
not chosen to implement either the most efficient code, instead preferring
to stick to a specific style of implementation. (The experiment is
documented at
http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/)

Curiously I revisited it today and tried it with pypy. The results were most
remarkable.

The python code as mentioned today required 112 μseconds on cPython 2.6
while it ended up requiring only 4.4 μseconds on PyPy (1.5 alpha).

What was even more curious was if one wrote the code differently eg. the
following alternative implementation as suggested in one of the comments.

def findlast(chainlength = 40, kill = 3):
    firstinc, c = 1, range(1,chainlength + 1)
    while len(c)>1:
        c, firstinc = [x for n,x in enumerate(c) if (firstinc+n) % 3], (n+1
+ firstinc) %3
    return c

import time
ITER = 100000
print findlast()
start = time.time()
for i in range(ITER):
    findlast()
end = time.time()
print 'Time per iteration = %s microseconds ' % ((end - start) * 1000000 /
ITER)

This code times in at about 31 μsec on cPython 2.6 vs 7.45 μseconds on PyPy

So - the code that was 4 times faster on cPython is now 60% as fast on PyPy.
Assuming more examples like this show up - I would imagine the definition of
efficient pythonic idiom to be evolving rather fast in a different direction
as pypy catches traction.

Dhananjay

-- 
----------------------------------------------------------------------------------------------------------------------------------
http://blog.dhananjaynene.com twitter: @dnene
<http://twitter.com/dnene>google plus:
http://gplus.to/dhananjaynene


More information about the BangPypers mailing list