Last active
December 22, 2015 08:18
-
-
Save crap0101/6443801 to your computer and use it in GitHub Desktop.
access list diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Cfr. http://rickystewart.wordpress.com/2013/09/03/why-sorting-an-array-makes-a-python-loop-faster/ | |
from __future__ import print_function | |
import random | |
import timeit | |
MAX = int(1e6) | |
ls = [x for x in range(MAX)] | |
lu = [x for x in range(MAX)] | |
random.shuffle(lu) | |
def foo(seq): | |
sum = 0 | |
for i in seq: | |
sum += i | |
return sum | |
def bar(seq): | |
for i in seq: | |
pass | |
if __name__ == '__main__': | |
setup = 'from __main__ import ls, lu, foo, bar' | |
rep = 500 | |
for f in ('foo', 'bar'): | |
for t, lst in (('slow', 'lu'), ('fast', 'ls')): | |
print("({}) {}: {:.5f}".format( | |
f, t, timeit.timeit('%s(%s)' % (f,lst), setup, number=rep)/rep)) | |
""" | |
crap0101@orange:/tmp$ python t.py # Python 2.7.3 | |
(foo) slow: 0.16535 | |
(foo) fast: 0.06115 | |
(bar) slow: 0.10698 | |
(bar) fast: 0.02427 | |
crap0101@orange:/tmp$ python3 t.py # Python 3.2.3 | |
(foo) slow: 0.19056 | |
(foo) fast: 0.07546 | |
(bar) slow: 0.10368 | |
(bar) fast: 0.02167 | |
crap0101@orange:/tmp$ ~/compilati/pypy3-2.1-beta1-linux64/bin/pypy t.py | |
(foo) slow: 0.34856 | |
(foo) fast: 0.05094 | |
(bar) slow: 0.00303 | |
(bar) fast: 0.00302 | |
crap0101@orange:/tmp$ ~/compilati/jython2.7b1/jython t.py | |
(foo) slow: 0.21446 | |
(foo) fast: 0.11107 | |
(bar) slow: 0.14249 | |
(bar) fast: 0.05194 | |
""" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment