power 2 in python using lshift

left shifting 1 in python does a pow 2 operation. I was just doing some performance comparison and here is an interesting result.

$ python -mtimeit 'pow(2,64)'
1000000 loops, best of 3: 1.18 usec per loop
12:31 AM:senthil@:~
$ python -mtimeit 'getattr(1,"__lshift__")(64)'
1000000 loops, best of 3: 0.356 usec per loop
12:32 AM:senthil@:~


I don't know the reason for this difference, I shall update the post when i find out why.

3 comments:

Anonymous said...

It makes sense in C/C++ where bitwise manipulations are fast. Dont know what can be the reason for python ?

Anonymous said...

Any reason you don't use the shorter, more obvious and 15 times faster 1<<64 or 2**64 ?

bluesmoon said...

it's your function lookup that's taking the time.