[Tutor] A Million Sevens

Tim Peters tim.peters at gmail.com
Sat Nov 18 02:33:01 CET 2006


[Thomas]
> Earlier today I typed the following into my pythonwin interactive
> interpreter in windows xp:
>
> >>> int('7' * 10 ** 6)
>
> I expected either an error message

Unlikely, if your box has enough RAM to run WinXP :-)

> or it to get stuck and require me to stop the process manually.

Not that either, but it will take a long time.  There are two
quadratic-time base conversions going on here, one to convert the
million-digit decimal string to a large binary integer, and then again
in the other direction to display the result as a decimal string.

In all, using Python 2.4.3 from a "DOS box" shell under WinXP on a
pretty fast box, this consumed about 16 minutes of CPU time, and
process memory use peaked at a relatively measly 11.3 MB.

> I read that unlike long integers in C, longs in python are only limited by
> the amount of memory (and virtual memory) your system has.

That's right.

> Can you guess what it did?

Nope.

> I'm temped to end the post here, but I'm new to this list and its possible
> that people might be annoyed by me not getting to the point within my
> initial post,

/Very/ good guess :-)

> so here's what it did:
>
> It thought about it for about 2 seconds then restarted my pc! explanations
> welcome.

Don't have one:  as above, it worked fine when I tried it.  I wasn't
using PythonWin, although hard to guess why that would matter.  I
wouldn't be surprised to see a GUI shell die a horrid death of some
kind when asked to display a million-character string, but 2 seconds
is far too short a time for your attempt to have gotten that far.

If you want to play, break it into two steps:

>>> i = int('7' * 10 ** 6)

That much does /only/ the decimal-string -> large binary integer part.
 Don't display i until the next step.  For display, it's /enormously/
faster to convert to a power-of-2 output base (instead of to decimal),
like

>>> print hex(i)

In more detail,

>>> from time import clock as now
>>> from math import log10
>>> log10(i)
999999.89085553051
>>> t = now(); ashex = hex(i); now() - t
0.0097427830685340843

That is, converting the big binary integer to a hex string took less
than a hundreth of a second, instead of the minutes required to
convert to a decimal string.

>>> len(ashex)
830485


More information about the Tutor mailing list