Calculate Big Number

N

Nac Temha

Hello,
How to *quickly* calculate large numbers. For example11258999068426240000000000000000000000000L




Regards.
 
S

Steven D'Aprano

Hello,
How to *quickly* calculate large numbers. For example
11258999068426240000000000000000000000000L


Timing how long that takes is trickier than it seems, because modern
versions of Python include a keyhole optimizer that will optimize some,
or all, of that calculation to constant(s). In Python 2.7:


py> import dis
py> code = compile("(10**25) * (2**50)", "", "single")
py> dis.dis(code)
1 0 LOAD_CONST 5 (10000000000000000000000000L)
3 LOAD_CONST 6 (1125899906842624L)
6 BINARY_MULTIPLY
7 PRINT_EXPR
8 LOAD_CONST 4 (None)
11 RETURN_VALUE


So, here's the best of five attempts to calculate the above in full, with
no optimizations, one hundred thousand times:

py> from timeit import Timer
py> t1 = Timer("(a**b)*(c**d)", setup="a,b,c,d = 10, 25, 2, 50")
py> min(t1.repeat(repeat=5, number=100000))
0.5256571769714355

So that's about 5 microseconds on my (slow) computer.


You can find the source code here:

http://hg.python.org/cpython/file/944e86223d1f/Objects/longobject.c
 
G

Gisle Vanem

Steven D'Aprano said:
py> from timeit import Timer
py> t1 = Timer("(a**b)*(c**d)", setup="a,b,c,d = 10, 25, 2, 50")
py> min(t1.repeat(repeat=5, number=100000))
0.5256571769714355

So that's about 5 microseconds on my (slow) computer.

That's pretty fast. So is there still a need for a GMP python-binding like
gmpy? http://code.google.com/p/gmpy/wiki/IntroductionToGmpy

GMP can include optimized assembler for the CPU you're using. But
I guess it needs more memory. Hence disk-swapping could be an issue
on performance.

--gv
 
C

casevh

That's pretty fast. So is there still a need for a GMP python-binding like
gmpy? http://code.google.com/p/gmpy/wiki/IntroductionToGmpy

GMP can include optimized assembler for the CPU you're using. But
I guess it needs more memory. Hence disk-swapping could be an issue
on performance.
gmpy will be faster than Python as the numbers get larger. The cutover varies depending on the platform, but usually occurs between 50 and 100 digits.

casevh
 
C

casevh

That's pretty fast. So is there still a need for a GMP python-binding like
gmpy? http://code.google.com/p/gmpy/wiki/IntroductionToGmpy

GMP can include optimized assembler for the CPU you're using. But
I guess it needs more memory. Hence disk-swapping could be an issue
on performance.
gmpy will be faster than Python as the numbers get larger. The cutover varies depending on the platform, but usually occurs between 50 and 100 digits.

casevh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top