Sunday, November 03, 2024

Re: bc cannot calculate very big number

On Sun, Nov 03, 2024 at 08:44:26AM +0100, Sebastien Marie wrote:

> Otto Moerbeek <otto@drijf.net> writes:
>
> > On Sun, Nov 03, 2024 at 08:13:20AM +0100, Otto Moerbeek wrote:
> >
> >> On Sun, Nov 03, 2024 at 09:38:47AM +0700, hahahahacker2009 wrote:
> >>
> >> > Hello,
> >> > OpenBSD bc(1) is unable to calculate very big number,
> >> > for example, (1024*1024)^(1024*1024) run for an hour and still cannot
> >> > give me the result.
> >> > It just use about 11M of memory and 100% CPU.
> >> > Gavin Howard's bc port do it in 2 minutes
> >> > GNU bc on Linux do it in 5 minutes.
> >>
> >> bc uses dc which does a simple exponentation computation, which is
> >> basically doing repeated multiplications. I'm sure there are smarter
> >> methods, it's not just implemented that way.
> >
> > Oh, I looked and I did it a bit smarter when I wrote that code 20
> > years back, but still, I think it can be improved.
> >
>
> The problem seems more on numnber printing than on number exponentation.
>
> $ /usr/bin/time -lp dc -e '1024 1024 * 1024 1024 * ^'
> real 46.39
> user 45.66
> sys 0.00
> 15244 maximum resident set size
> 0 average shared memory size
> 0 average unshared data size
> 0 average unshared stack size
> 3509 minor page faults
> 1 major page faults
> 0 swaps
> 0 block input operations
> 0 block output operations
> 0 messages sent
> 0 messages received
> 0 signals received
> 0 voluntary context switches
> 3334 involuntary context switches
>
> The exponentation took ~50 seconds (dc(1) doesn't print the number on
> the stack by default).
> --
> Sebastien Marie

Heh, thanks for that insight. I might take a look some day.

-Otto

No comments:

Post a Comment