[Tutor] A Million Sevens

Luke Paireepinart rabidpoobear at gmail.com
Sat Nov 18 03:39:27 CET 2006

Chris Hengge wrote:
> Not that it changes your reply, but just for my own sanity:
> int('7' * 10 ** 6) <- does this not just type-cast a char into an int?
it typecasts a 1,000,000 character long string to an integer.
> Meaning that rather then consuming 1024k as you stated, it would 
> consume 2048k at the peak of the calculation(2bytes per char? * 1m = 
> 2048k)
um, I'm not sure how you came up with these numbers.
Obviously there are other considerations here besides the amount of 
memory required to store the data (like the memory used by the string 
class, and the fact that strings are probably stored as a linked list, 
so 1 byte for the char, and 4 bytes for the char pointer).  If you just 
considered the characters, they're only 1 byte, not 2, assuming you're 
using regular ascii characters (0x00 - 0xff is 255 unique bit strings, 
and since english and other latin-based languages only need 26*2 (lower 
+ uppercase letters) + 10 for numbers, and various other symbols, 255 is 
plenty.), so it'd be 1024k of memory usage.  if you considered the 
pointers also, it'd be 5116k.  and various other things raise this 
memory usage more.

 >then typecasting to int would drop it back down to 1k (1byte per int * 
1m = 1024k
no, typecasting it to int won't drop it to 1k.
What's happening is that, inside the function call (the 'int' type 
a 7-million character string, i.e. '77777777777777777777777777............7'
is generated, and then the 'int()' call converts it to the equivalent 
Not to a million different 7 integers.
Do you see this?  It's only one number.
and I don't know where you got 1 byte per int.  Even if they were 
1,000,000 individual sevens, the C library reserves the same amount of 
memory for an int no matter what size it is,
so even though 7 is a low number, there'd still be 4 bytes of usage per 
int (depending on the implementation)
Depending on how exactly Python stores long ints, it should use 
substantially less memory than the equivalent character string would.

Recall that a 2 byte unsigned integer can hold all numbers less than 
2**16 (65536), a
4 byte (the default long-int size) unsigned integer can hold all numbers 
less than 2**32 (4294967296)
which is ten decimal digits long.  So you see that the memory usage of 
the python long integer is going to be much smaller than the usage of 
the character string that represents it.
The string's size usage grows linearly with the data it represents ('7' 
will take x memory, '77' will take x*2 memory, '777' will take x*3 memory)
while the size of the integer grows much faster than the memory usage 
memory usage
(0-256 takes 1 byte, 0-65536 takes 2 bytes, 0-16777216 takes 3 bytes, 
0-4294967296 takes 4 bytes, any number less than 308 digits long takes 
128 bytes)

> So, just for sake of getting to a point since I missed the one by the 
> original poster.. why would you not convert that 7 from a char to an 
> int first? That calculation is almost instant, and it doesn't require 
> the memory rollercoaster that this calculation would require..
Because, that's not what's happening.  He's not converting 1,000,000 '7' 
chars to the integer 7,
he's converting a string of 1,000,000 7s into an integer with 1,000,000 
digits, all of which are 7.

Consider this example:
 >>> '7' * 5

You see, it's a string of length 5 with the contents just the original 
character, '7', repeating.
You can do something similar with a string rather than a character,
 >>> 'hello' * 3

Now consider this:
 >>> '7' * 2 ** 5

In the order of operations for Python, exponents come before 
multiplications, so this is equivalent to
'7' * 32
as evidenced by:
 >>> len('7' * 2 ** 5)

Hope that helps and makes sense,

> Anyways.. back to the poster...
> Perhaps you wanted an error like this?
> print '=' * 1000000000
> Traceback (most recent call last):
>   File "<input>", line 1, in ?
> MemoryError
> On 11/17/06, *Luke Paireepinart* <rabidpoobear at gmail.com 
> <mailto:rabidpoobear at gmail.com>> wrote:
>     Chris Hengge wrote:
>     > I'm thinking you either have a problem with a memory leak (my memory
>     > isn't changing, just at 100% CPU), or your CPU overheated from poor
>     > cooling since it is at 100% utilization.
>     yeah I second this...
>     there's no reason why this would reboot your computer.
>     At Chris: It's building a string that's 1,000,000 characters long,
>     so it
>     should be increasing your memory usage by at least 1,000,000
>     characters,
>     or 1 mb.  But that's over a probably long period of time, so you just
>     didn't notice any change.
>     >
>     > On 11/17/06, *Chris Hengge* <pyro9219 at gmail.com
>     <mailto:pyro9219 at gmail.com>
>     > <mailto: pyro9219 at gmail.com <mailto:pyro9219 at gmail.com>>> wrote:
>     >
>     >     Well, I dont get the point.. its not locking up my system or
>     >     anything.. its just crunching away... even while I type this...
>     >     I guess your point is that it should stop since a 32 bit O/S can
>     >     only count to:
>     >     4,294,967,296
>     >
>     >     On 11/17/06, *Thomas* < tavspam at gmail.com
>     <mailto:tavspam at gmail.com>
>     >     <mailto:tavspam at gmail.com <mailto:tavspam at gmail.com>>> wrote:
>     >
>     >         Earlier today I typed the following into my pythonwin
>     >         interactive interpreter in windows xp:
>     >
>     >         >>> int('7' * 10 ** 6)
>     >
>     >         I expected either an error message or it to get stuck and
>     >         require me to stop the process manually.
>     >
>     >         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.
>     >
>     >         Can you guess what it did?
>     >
>     >         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, so here's what it did:
>     >
>     >         It thought about it for about 2 seconds then restarted
>     my pc!
>     >         explanations welcome.
>     >

More information about the Tutor mailing list