2**HUGENUMBER Why not optimise it?

Tim Peters tim.one at comcast.net
Wed May 22 20:51:16 EDT 2002


[Mike C. Fletcher]
> I've noticed the use of 2**HUGENUMBER as a speed test a couple of times
> now.  I'm curious why the language(s) doesn't (don't) optimise 2**X
> operations for really huge exponents.  Python obviously optimises 1**x
> (since 1**1000000 returns instantly), so why not 2**x where x >
> (30 or 62)?
>
> i.e.  return 1L<<exponent

Why bother?  i**j is performed by a general algorithm that does a number of
long-int multiplications, but no more than 2*ceiling(log_base_2(j)) of them.
i==1 is not a special case; it zooms simply because all of the O(log(j))
intermediate products are exactly 1, so go very quickly.

> I'm guessing the reason is "no one really uses this much, so there's not
> much point putting the effort into the change", but I'm still curious if
> I'm missing something about 2-exponentiation.

No, there's simply no point to burning code space to special-case power-of-2
bases; if you know your base is a power of 2, feel free to shift yourself --
I do <wink>.






More information about the Python-list mailing list