2**HUGENUMBER Why not optimise it?

George Demmy gdemmy at layton-graphics.com
Wed May 22 15:21:50 EDT 2002


"Mike C. Fletcher" <mcfletch at rogers.com> writes:

> 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 guess I didn't do a good job of making the point that the "speed" of
a language and its implementation depends hugely on context. I was
able to go from problem to solution in less than two seconds (I'm not
the worlds fastest typest...) for the toy problem. Execution time was
less than half a second, the other second and a half being spent
typing

python -c 'print 2**10000'

OK, I lied. Probably closer to five seconds typing, but it better
makes the point. Using clisp (in my limited experience, a *much*
faster bignum implementation, btw, than Guido Python), I could have
shaved four tenths of a second... either way, execution time was a
small fraction of total execution time.

> 
> i.e.  return 1L<<exponent
> 
> 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.

Probably not missing anything about 2-exponentiation. 7 ** 10000 would
have been find. I confess, I "rigged" it to eliminate the obvious Perl
one-liner. Perl would the spit out a floating point answer just
fine. But I'm unrepentant in the rigging -- "Is python fast 'enough'?" 
is a loaded question!

> Given that the test was stated as an English word problem, the
> 1L<<10000 solution would probably even be acceptable in the speed
> tests.  We just need to restate the problem as:
> 
>  "What is the integer/long value of 2 raised to the power of
>  10000000?"

Problems are usually specifed in words... or less! I always get a
chuckle (in the gallows humor sense) when I see the folks trying to
elicit just what it is that the customer wants. Much of my time as a
programmer is spent exploring problems and trying to nail down specs
not hacking :(

How long does it take to fire up emacs (OK, zero, it's *always*
running), edit the c code, compile, and exectute. Again, illustrating
that "speed" can (and should!) be measured more ways than one.

> And Python will be universally acknowledged as the speediest language
> on the planet ;) .
> 
> Mike

Wink aside, if you're interested in flat out execution speed for
bignums, you might check out clisp... Optimization of 2**HUGEL may be
premature at this point. I like Python and the Guido Python
implementation because it lets me go from problem statement to
solution quickly for a wide variety of problems... small and
large.

G



More information about the Python-list mailing list