Skip to content

Instantly share code, notes, and snippets.

@lastland
Created December 20, 2012 10:23
Show Gist options
  • Save lastland/4344462 to your computer and use it in GitHub Desktop.
Save lastland/4344462 to your computer and use it in GitHub Desktop.
The built-in function "sum" of Python 2.7 does run faster. But it's not true in PyPy.
import itertools
# tstime(https://bitbucket.org/gsauthof/tstime) is used to measure the performance.
# OS: Archlinux 3.6.10-1
# Python Version: 2.7.3
# PyPy Version: 2.0.0-beta1
c = 0
for (i, j) in itertools.product(range(10000), repeat=2):
c += i * j
print c
# Python2 Result:
# 2499500025000000
#
# Exit status: 0
#
# pid: 513 (python2) started: Thu Dec 20 17:50:00 2012
# real 44.859 s, user 44.787 s, sys 0.010s
# rss 3556 kb, vm 9424 kb
#
# PyPy Result:
# 2499500025000000
#
# Exit status: 0
#
# pid: 517 (pypy) started: Thu Dec 20 18:33:55 2012
# real 16.566 s, user 16.463 s, sys 0.030s
# rss 19424 kb, vm 66876 kb
print sum([i * j for (i, j) in itertools.product(range(10000), repeat=2)])
# Python2 Result:
# 2499500025000000
#
# Exit status: 0
#
# pid: 555 (python2) started: Thu Dec 20 17:51:58 2012
# real 40.591 s, user 39.310 s, sys 1.223s
# rss 1575516 kb, vm 1610280 kb
#
# PyPy Result:
# 2499500025000000
#
# Exit status: 0
#
# pid: 499 (pypy) started: Thu Dec 20 18:33:13 2012
# real 23.189 s, user 18.393 s, sys 1.180s
# rss 812232 kb, vm 859600 kb
def f():
for (i, j) in itertools.product(range(10000), repeat=2):
yield i * j
print sum(f())
# Python2 Result:
# 2499500025000000
#
# Exit status: 0
#
# pid: 538 (python2) started: Thu Dec 20 17:51:19 2012
# real 32.456 s, user 32.393 s, sys 0.003s
# rss 3624 kb, vm 9424 kb
#
# PyPy Result:
# 2499500025000000
#
# Exit status: 0
#
# pid: 529 (pypy) started: Thu Dec 20 18:34:15 2012
# real 20.083 s, user 20.027 s, sys 0.027s
# rss 19432 kb, vm 67008 kb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment