Article 5393 of comp.lang.perl:
Xref: feenix.metronet.com comp.lang.perl:5393
Newsgroups: comp.lang.perl
From: timbo@ig.co.uk (Tim Bunce)
Path: feenix.metronet.com!news.ecn.bgu.edu!psuvax1!uwm.edu!math.ohio-state.edu!cs.utexas.edu!uunet!news!demon!ig.co.uk!timbo
Subject: Re: What's the difference?
References: <MCAMPBEL.93Aug16140708@debussy.sbi.com> <1993Aug19.122152.7130@colorado.edu> <CC0vCM.BwE@ig.co.uk> <1993Aug25.135401.14146@colorado.edu>
Organisation: Paul Ingram Group, Software Systems, +44 483 424424
Date: Thu, 26 Aug 1993 18:13:25 +0000
Message-ID: <CCDp98.IB9@ig.co.uk>
Sender: usenet@demon.co.uk
Lines: 74

From the keyboard of tchrist@cs.colorado.edu (Tom Christiansen):
>From the keyboard of timbo@ig.co.uk (Tim Bunce):
>:join:    52 cpu seconds 
>:interp:  60 cpu seconds 
>:concat:  95 cpu seconds 
>:sprintf: 75 cpu seconds 
>
>Try it on Perl5.
>
>--tom
>    Tom Christiansen      tchrist@cs.colorado.edu       303-444-3212

Ok, here it is...

Roy Johnson <rjohnson@shell.com> kindly pointed out that I wasn't quite fair
(in my original post) to interpolation and sprintf, as I made them print commas
when join and concat didn't have to.

So I've modified the tests slightly and rerun them for perl4 and perl5:


Interp:   $x = "$a::$b::$c::$d::$e\n"
Join:     $x = join('',$a,'::',$b,'::',$c,'::',$d,'::',$e,"\n")
Concat:   $x = $a.'::'.$b.'::'.$c.'::'.$d.'::'.$e."\n"
Sprintf:  $x = sprintf("%s::%s::%s::%s::%s\n", $a,$b,$c,$d,$e);

Results:  Perl4    Perl5a2

Interp:   54.52 => 72.38  cpu seconds 
Join:     60.27 => 67.32  cpu seconds 
Concat:  136.88 => 73.18  cpu seconds 
Sprintf:  68.58 => 77.60  cpu seconds 

Note that the Perl4 is a fully optimised binary whereas the Perl5 is the
stock out-of-the-box sparc binary built by Larry with no optimisation at all.
If anyone has rebuilt perl5 with optimisation I'd be VERY interested in their
own perl4 vs perl5 figures.


For the curious, here's the code I used:

#!/usr/local/bin/perl

($a,$b,$c,$d,$e) = ('1234567890') x 5;
$loop = 1000000;
print "\n";

&trial("Interp", <<'END');
    $x="$a::$b::$c::$d::$e\n"
END

&trial("Join", <<'END');
    $x=join('',$a,'::',$b,'::',$c,'::',$d,'::',$e,"\n")
END

&trial("Concat", <<'END');
    $x=$a.'::'.$b.'::'.$c.'::'.$d.'::'.$e."\n"
END

&trial("Sprintf", <<'END');
    $x=sprintf("%s::%s::%s::%s::%s\n", $a,$b,$c,$d,$e);
END


sub trial{
    local($title, $code) = @_;
    eval join('','($u1,$s1)=times; $i=0;',
                 'while($i++ < $loop){', $code, '};',
                 '($u2,$s2)=times');
    printf("$title: %.2f cpu seconds $@\n\n", ($u2+$s2)-($u1+$s1));
}

Regards,
Tim Bunce.


