Out of curiosity ran the code in Python 3.2 and Python 2.7. Python 2.7: 30 μsec Python 3.2: 35 μsec
Regards, Chetan On Thu, Aug 11, 2011 at 3:47 PM, Dhananjay Nene <[email protected]>wrote: > 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 > _______________________________________________ > BangPypers mailing list > [email protected] > http://mail.python.org/mailman/listinfo/bangpypers > -- Regards, Chetan http://technobeans.com _______________________________________________ BangPypers mailing list [email protected] http://mail.python.org/mailman/listinfo/bangpypers
