Re: PERL as a first programming language?

Logan Shaw (logan@taligent.com)
Thu, 28 Jul 1994 19:12:22 GMT

In article <RSANDERS.94Jul24231449@hrothgar.mindspring.com>, rsanders@mindspring.com (Robert Sanders) writes:
> > * It uses unlimited precision, so there is no need to discuss the
> > problems of word size or machine representation (what other
> > language will give the correct answer to "write 2**10001" in
> > about 1 second?)
>
> Off the top of my head, Python (one implementation freely available),
> Lisp (CLISP, GCL, FEEL, XLISP freely available), and Scheme (scm,
> vscm, elk, STk and more freely available). It also takes much less
> than one second real time for most of those to give the answer on my
> 486dx33:
>
> winners:
> STk: 0.23 sec
> clisp: 0.35 sec
> scm: 0.47 sec
> elk: 0.57 sec
> vscm: 0.65 sec
> losers:
> python: 2.28 sec (only .24s to calculate; like GCL, bad printer)
> GCL: 12.00 sec (it only takes .10s to calculate; GCL's
> bignum printer is a big loser)

Just to be fair, I should mention that I tried *really* calculating
2 * 10001 in Perl. I used the "bigint.pl" library that comes with the
distribution, but it doesn't have a power function, so I quickly
hacked one in terms of the functions in bigint.pl.

This is the code:

#! /usr/local/bin/perl

require "bigint.pl";

sub bpow
{
local ($num, $exponent) = @_;
local ($answer) = '1';

while ($exponent)
{
if ($exponent & 0x01)
{
# this exponent of the number is a factor
# in the answer, so multiply it in.
$answer = &bmul ($answer, $num);
}

# go up to the next factor which is a power of two
$num = &bmul ($num, $num);

$exponent >>= 1;
}

$answer;
}

($base, $power) = @ARGV;
print "$base ** $power ==\n";
print &bpow ($base, $power), "\n";

And, on an RS/6000 250 (which is a PowerPC 601):

$ time ./power 2 10001
2 ** 10001 ==
[...] 5193418752

real 0m40.01s
user 0m39.73s
sys 0m0.13s

Not bad, considering it's using floating point to do the integer
stuff, which it's using (in arrays) to do the functions in bigint.pl,
which I'm calling over and over again to do my bpow() function.

If I'd written bpow from scratch, I'm sure it could have been much
faster.

Adios,
Logan

-- 
The genius of France can be seen at a glance
And it's not in their fabled fashion scene
It's not that they're mean, or their wine, or cuisine
I refer of course to the guillotine
(the French knew how to lynch)
                T-Bone Burnett, "I Can Explain Everything"