Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #36384 > unrolled thread

Calculate Big Number

Started byNac Temha <naccttemha@gmail.com>
First post2013-01-08 02:44 +0200
Last post2013-01-08 08:33 -0800
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Calculate Big Number Nac Temha <naccttemha@gmail.com> - 2013-01-08 02:44 +0200
    Re: Calculate Big Number Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-08 05:50 +0000
      Re: Calculate Big Number Gisle Vanem <gvanem@broadpark.no> - 2013-01-08 11:06 +0100
        Re: Calculate Big Number casevh@gmail.com - 2013-01-08 08:33 -0800
        Re: Calculate Big Number casevh@gmail.com - 2013-01-08 08:33 -0800

#36384 — Calculate Big Number

FromNac Temha <naccttemha@gmail.com>
Date2013-01-08 02:44 +0200
SubjectCalculate Big Number
Message-ID<mailman.244.1357605853.2939.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Hello,
How to *quickly* calculate large numbers. For example
>>> (10**25) * (2**50)
11258999068426240000000000000000000000000L




Regards.

[toc] | [next] | [standalone]


#36414

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-01-08 05:50 +0000
Message-ID<50ebb3c1$0$30003$c3e8da3$5496439d@news.astraweb.com>
In reply to#36384
On Tue, 08 Jan 2013 02:44:04 +0200, Nac Temha wrote:

> Hello,
> How to *quickly* calculate large numbers. For example
>>>> (10**25) * (2**50)
> 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



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#36422

FromGisle Vanem <gvanem@broadpark.no>
Date2013-01-08 11:06 +0100
Message-ID<mailman.269.1357639597.2939.python-list@python.org>
In reply to#36414
"Steven D'Aprano" <steve+comp.lang.python@pearwood.info> wrote:

> 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

[toc] | [prev] | [next] | [standalone]


#36439

Fromcasevh@gmail.com
Date2013-01-08 08:33 -0800
Message-ID<ce5ef123-b971-4cf9-8bcd-24e9bb05a662@googlegroups.com>
In reply to#36422
On Tuesday, January 8, 2013 2:06:09 AM UTC-8, Gisle Vanem wrote:
> "Steven D'Aprano" <email deleted> wrote:
> 
> > 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.
> 
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
> 
> --gv

[toc] | [prev] | [next] | [standalone]


#36440

Fromcasevh@gmail.com
Date2013-01-08 08:33 -0800
Message-ID<mailman.282.1357662793.2939.python-list@python.org>
In reply to#36422
On Tuesday, January 8, 2013 2:06:09 AM UTC-8, Gisle Vanem wrote:
> "Steven D'Aprano" <email deleted> wrote:
> 
> > 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.
> 
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
> 
> --gv

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web