[comp.lang.perl] Slower in numerical calculation than awk?

lixj@acf4.NYU.EDU (Xiaojian Li) (02/04/91)

I write small awk and perl program, and to my dismay, awk is almost
twice as fast as perl,

---awk---
#!/bin/awk -f
BEGIN { x = 1.0
        for(i=0;i<100000;++i) x += i
}
END { print x }

4.99995e+09
5.6u 0.0s 0:05 100% 0+280k 0+0io 0pf+0w

---perl---                            Both are run on SPARC 1
#!/usr/local/bin/perl
$X = 1.0;
for ($i = 0; $i < 100000; ++$i) {
    $X += $i;
}
print $X, "\n";

4999950001
10.6u 0.1s 0:10 100% 0+428k 0+0io 0pf+0w

I notice that perl gives more accurate result. Is this higher precision
which causes slowing down? In what precision perls does math?

tchrist@convex.COM (Tom Christiansen) (02/04/91)

From the keyboard of lixj@acf4.NYU.EDU (Xiaojian Li):
:I write small awk and perl program, and to my dismay, awk is almost
:twice as fast as perl,

That depends.  See below.

:#!/bin/awk -f
:BEGIN { x = 1.0
:        for(i=0;i<100000;++i) x += i
:}
:END { print x }

:I notice that perl gives more accurate result. Is this higher precision
:which causes slowing down? In what precision perls does math?

Perl uses double precision math; old awk, at least, used single precision
math.  Here are my numbers (5-run averages) from a Convex C-220:

    awk:   7.4u 
    gawk:  7.5u
    nawk:  5.9u
    perl:  5.9u

So it would appear that it varies by architecture.  One anomaly in
my timings is that they all took negligle system time, except for
gawk, which consistently took 8.7 system time!!!

Maybe a Convex is just optimized for perl. :-)  [Or double precision
math, which would be easier to believe.]

--tom
--
"Still waiting to read alt.fan.dan-bernstein using DBWM, Dan's own AI
window manager, which argues with you 10 weeks before resizing your window." 
### And now for the question of the month:  How do you spell relief?   Answer:
U=brnstnd@kramden.acf.nyu.edu; echo "/From: $U/h:j" >>~/News/KILL; expire -f $U

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (02/05/91)

In article <2540004@acf4.NYU.EDU> lixj@acf4.NYU.EDU (Xiaojian Li) writes:
: I write small awk and perl program, and to my dismay, awk is almost
: twice as fast as perl,
: 
[examples deleted]
:
: I notice that perl gives more accurate result. Is this higher precision
: which causes slowing down? In what precision perls does math?

Perl uses double precision, but so does awk.  You're just running afoul of
awk's default output format.  The primary reason Perl is slower in numerics
is simply that I've never put much effort into optimizing them.  As the man
page says right up front, Perl is optimized for text processing.  C has to
be good for something, after all...

Someday I hope to switch to something more like threaded code, and if I do,
numerics will be much faster.

Larry

aisaka@cac.co.jp (Aisaka Satoshi) (02/06/91)

Hi! I'm Satoshi Aisaka from Japan.

In article <9102040846.AA17752@acf4.NYU.EDU> lixj@acf4.NYU.EDU (Xiaojian Li) writes:
 |I write small awk and perl program, and to my dismay, awk is almost
 |twice as fast as perl,
 |---awk---
 |#!/bin/awk -f
 |BEGIN { x = 1.0
 |	  for(i=0;i<100000;++i) x += i
 |}
 |END { print x }
 |
 |4.99995e+09
 |5.6u 0.0s 0:05 100% 0+280k 0+0io 0pf+0w
 |
 |---perl---                            Both are run on SPARC 1
 |#!/usr/local/bin/perl
 |$X = 1.0;
 |for ($i = 0; $i < 100000; ++$i) {
 |    $X += $i;
 |}
 |print $X, "\n";
 |
 |4999950001
 |10.6u 0.1s 0:10 100% 0+428k 0+0io 0pf+0w
 |
 |I notice that perl gives more accurate result. Is this higher precision
 |which causes slowing down? In what precision perls does math?

I tried the same test and got different result.
My results are:

--- awk ---

4.99995e+09
5.3u 0.0s 0:05 99% 20+18k 0+0io 0pf+0w


--- perl ---

4999950000
4.7u 0.0s 0:04 99% 46+18k 0+1io 0pf+0w

perl is faster than awk on my machine!

awk	:GNU awk 2.11 (modified for Japanese)
perl	:Ver3.00 pl.44 (modified for Japanese)
machine	:SONY NEWS-3860 (with 20MHz R3000,80M real-Memory)

--
AISAKA, Satoshi (aisaka%cac.co.jp@uunet.uu.net) 
R&D for Advanced Technologies, Computer Applications Co., Ltd.
2-3-1 Hitotsubashi Chiyoda-Ku Tokyo 101 Japan
Phone (+3)3263-7241  fax(+3)3263-4873