Created
April 11, 2012 14:11
-
-
Save melpomene/2359537 to your computer and use it in GitHub Desktop.
Compare bit shift and division in Python
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
""" | |
Why is division by two slower than bitshift in python? | |
Should the "compiler" represent this in the same way. | |
> python shiftordivide.py | |
Divided mean time took 0.027 | |
Shift mean time took 0.008 | |
>pypy shiftordivide.py --OJit | |
Divided mean time took 0.034 | |
Shift mean time took 0.006 | |
Shift quicker but div slower with JIT compiler? | |
""" | |
import time | |
divlist = [] | |
for i in xrange(100): | |
i = 2**10000 | |
t1 = time.time() | |
while i != 2: | |
i = i / 2 | |
t2 = time.time() | |
divlist.append(t2-t1) | |
tot = 0 | |
for t in divlist: | |
tot += t | |
print "Divided mean time took %0.3f" % (tot/len(divlist)) | |
divlist = [] | |
for i in xrange(100): | |
i = 2**10000 | |
t1 = time.time() | |
while i != 2: | |
i = i >> 1 | |
t2 = time.time() | |
divlist.append(t2-t1) | |
tot = 0 | |
for t in divlist: | |
tot += t | |
print "Shift mean time took %0.3f" % (tot/len(divlist)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment