[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

RE: [FWP] beauty vs. brains



> From: Jeff Pinyan [mailto:jeffp@crusoe.net]
> Sent: Thursday, March 16, 2000 06:53
> To: Dave W. Smith
> Cc: fwp@technofile.org
> Subject: Re: [FWP] beauty vs. brains
>
>
> On Mar 15, Dave W. Smith said:
>
> >>   $file = do { local $/, <FILE> };
> >>   read(FILE, $file, -s FILE);
> >
> >Perl's sysread() uses a single kernel read(), at least on FreeBSD.
>
> I was pondering the use of sysread(), but I didn't do any benchmarks.

No difference.

Judging by the lengths, I am indeed reading the file into the scalar.
Note that the file is exactly 100_000 lines long (Windows/DOS), so the
string length is 100_000 characters fewer.

['too few iterations' warnings removed.]

Length = 3505785
Benchmark: timing 1 iterations of Do0, Do1, Slurp0, Slurp1, Sysread,
XRead...
3405785       Do0: 31 wallclock secs (13.86 usr + 16.56 sys = 30.42 CPU)
3405785       Do1: 33 wallclock secs (15.09 usr + 16.44 sys = 31.53 CPU)
3405785    Slurp0: 29 wallclock secs (10.62 usr + 18.05 sys = 28.67 CPU)
3405785    Slurp1:  1 wallclock secs ( 0.33 usr +  0.08 sys =  0.41 CPU)
3405785   Sysread:  0 wallclock secs ( 0.17 usr +  0.14 sys =  0.31 CPU)
3405785     XRead:  1 wallclock secs ( 0.16 usr +  0.11 sys =  0.27 CPU)

#!/usr/local/bin/perl -w
use strict;
use Benchmark;

open IN, 'e:/sorting/ips.txt' or die $!;
seek IN, 0, 0;
print "Length = ", -s IN, "\n";

timethese (1 << (shift || 0), {
  Do0     => 'seek IN, 0, 0; my $x = do { local $/; <IN> };
	            print length $x',
  Do1     => 'seek IN, 0, 0; my $x = "x" x -s IN; $x = do { local $/;
<IN> };
	            print length $x',
  Slurp0  => 'seek IN, 0, 0; my $x; { local $/; $x = <IN> };
	            print length $x',
  Slurp1  => 'seek IN, 0, 0; my $x = "x" x -s IN; { local $/; $x =
<IN> };
	            print length $x',
  Sysread => 'seek IN, 0, 0; sysread IN, my $x, -s IN; print length $x',
  XRead   => 'seek IN, 0, 0; read IN, my $x, -s IN; print length $x',
});

So the conclusion seems to be that preallocation of the scalar holding
the result helps the non-'do' version of the conventional slurp
enormously.  But 'read' or 'sysread' are still the winners, by 100 to 1
over the way many of us have supported.

--
Larry Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com



==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe