In [105]:
import sys
sys.path.append('/Users/thatch/code/sre_yield_thatch')
import fastdivmod
reload(fastdivmod)
import time
import math
In [106]:
# divmod_iter gives you the moduli when divided by 256 here (right to left)
list(fastdivmod.divmod_iter(sys.maxint, 256))
Out[106]:
[255, 255, 255, 255, 255, 255, 255, 127]
In [107]:
a = sys.maxint**10000
b = 10**1024
In [108]:
t0 = time.time()
a_ = a
while a_:
    mod = a_ % b
    a_ = a_ / b
t1 = time.time()
print t1 - t0
# but that's cheating, we can get a 2x speedup by just using divmod
# ... anyone know why? yell it out.
0.731560945511

In [109]:
t0 = time.time()
a_ = a
while a_:
    a_, mod = divmod(a_, b)
t1 = time.time()
print t1 - t0
0.368988990784

In [110]:
print "Gur / naq % bcrengbef ner abj qrsvarq va grezf bs qvizbq(). AHYY pna or cnffrq sbe cqvi be czbq, va juvpu pnfr gung cneg bs gur erfhyg vf fvzcyl guebja njnl.".decode('rot-13') + ' (in longmodule.c)'
The / and % operators are now defined in terms of divmod(). NULL can be passed for pdiv or pmod, in which case that part of the result is simply thrown away. (in longmodule.c)

In [111]:
t0 = time.time()
for i in fastdivmod.divmod_iter(a, b):
    pass
t1 = time.time()
print t1 - t0
# hey guess what it's the same (look in fastdivmod.py)
0.36629486084

In [112]:
# switches when < ~1000 iterations to divmod(), and we only have...
print "base **", math.log(a, b)
base ** 185.204001239

In [113]:
b = 2
a = b ** 100000
In [114]:
print math.log(a, 256), "bytes"
print "base **", math.log(a, b)
12500.0 bytes
base ** 100000.0

In [115]:
t0 = time.time()
a_ = a
while a_:
    a_, mod = divmod(a_, b)
t1 = time.time()
with_divmod = t1 - t0
print with_divmod
1.98341202736

In [116]:
t0 = time.time()
for i in fastdivmod.divmod_iter(a, b):
    pass
t1 = time.time()
with_fastdivmod = t1 - t0
print with_fastdivmod
0.0662319660187

In [117]:
print "speedup %.02fx" % (with_divmod/with_fastdivmod,)
speedup 29.95x

In [117]: