Re:Low Level Bit Hacks You Absolutely Must Know
by comatose_kid on Tuesday June 30, 2009 @05:13PM
(#681594)
Surprisingly (I just benchmarked it myself), the times are near the same.
Here's my benchmark (in case I made a mistake):
import time
def combine(upper,lower): return (2**32)*upper + lower
def combine2(upper,lower): return (upper << 32) | lower
def test_combine():
start = time.clock()
for i in range(10000000):
combine(i,i)
end = time.clock()
output = 'The time taken for combine() is ' + str(end - start)
print output
def test_combine2():
start = time.clock()
for i in range(10000000):
combine2(i,i)
end = time.clock()
output = 'The time taken for combine() is ' + str(end - start)
print output
Re:Low Level Bit Hacks You Absolutely Must Know
by tptacek on Tuesday June 30, 2009 @09:16PM
(#681926)
That's surprising; it's more than twice as slow in Ruby (yeah, yeah). It's also (for obvious reasons) orders of magnitude faster in C --- if you were crazy enough to actually write code that way.
Re:Low Level Bit Hacks You Absolutely Must Know
by ramchip on Wednesday July 01, 2009 @10:08AM
(#682647)
I don't know much about Python so forgive me if this is stupid - but could 2^32 have been optimized as a constant, so that the result is just a multiplication and an addition?
Here's my benchmark (in case I made a mistake):
Also see http://wiki.python.org/moin/PythonSpeed/PerformanceTips#Pyth...