str(bigint) is slow

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Fri Jul 9 02:34:03 EDT 2004


Bryan wrote:

> does anyone know how to make this faster?  it seems that str(x) is
> the slow part. 
> 
> 
>  >>> def foo():
> ...     t1 = time.time()
> ...     x = 19 ** 314159
> ...     t2 = time.time()
> ...     y = str(x)
> ...     t3 = time.time()
> ...     print y
> ...     t4 = time.time()
> ...     print t2-t1, t3-t2, t4-t3
> ...
>  >>> foo()
> <snip a print out of a billion numbers>
> 3.78499984741 230.490999937 0.0700001716614
Bryan wrote:

> does anyone know how to make this faster?  it seems that str(x) is
> the slow part. 
> 
>  >>> def foo():
> ...     t1 = time.time()
> ...     x = 19 ** 314159
> ...     t2 = time.time()
> ...     y = str(x)
> ...     t3 = time.time()
> ...     print y
> ...     t4 = time.time()
> ...     print t2-t1, t3-t2, t4-t3
> ...
>  >>> foo()
> <snip a print out of a billion numbers>
> 3.78499984741 230.490999937 0.0700001716614

401732 digits actually ... that's not even half a million ...

There was a recent thread titled "How to get decimal form of largest
known prime?" ...

http://tinyurl.com/3b2no

http://groups.google.com.au/groups?hl=en&lr=&ie=UTF-8&threadm=2is27mFqee
n8U1%40uni-berlin.de&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%
26selm%3D2is27mFqeen8U1%2540uni-berlin.de

Tim Delaney
Tim Delaney



More information about the Python-list mailing list