Tuesday, December 13, 2011

Okay, timed it..

Hunh, it's actually faster to multiply on the fly than to look up a precomputed value.  Weird.

import timeit

setup = '''
y = []
for x in range(80):
    y.append(x*32)
'''

t = timeit.Timer(stmt="a = 80*33", setup=setup)
print t.timeit()

t = timeit.Timer(stmt="a = 80<<5", setup=setup)
print t.timeit()

t = timeit.Timer(stmt="a = y[79]", setup=setup)
print t.timeit()

So yeah, that gives me the result:
0.0310996928381
0.03094031504
0.049108406236

Some miniscule advantage with bit shifting (I multiplied by 33 on purpose so it wouldn't bit shift behind the scenes.. though maybe it does then adds 1... :-/ who knows.) but yeah, there we have it.

No comments:

Post a Comment