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

Re: [FWP] beauty vs. brains



True, you have to use something like (NT command line):

W:\>perl -e "open(FILE,\"test.txt\"); while(read(FILE,$buf,0xffff)) {$file 
.= $buf;} print $file;"

because at some point the C code must be using "read" or "fgets", which 
require allocated buffers equal to their length limits.

Using the following (on NT)

#!/usr/bin/perl

use Benchmark 'timethese';

$fn = "test.txt";

print "Size of $fn: ", -s $fn, "\n";

timethese(100, {
   'local' => q{
     open FILE, $fn;
     { local $/, $f = <FILE> }
     close FILE;
   },
   'do+local' => q{
     open FILE, $fn;
     $f = do { local $/, <FILE> };
     close FILE;
   },
   'read' => q{
     open FILE, $fn;
     read(FILE,$f,-s FILE);
     close FILE;
   },
   'buf_read' => q{
     open FILE, $fn;
     while(read(FILE,$buf,0xffff)) {
       $file .= $buf;
     }
   }
});

for small a file (1000 iterations) I get:

Size of test.txt: 148
Benchmark: timing 1000 iterations of buf_read, do+local, local, read...
   buf_read:  1 wallclock secs ( 0.14 usr +  0.50 sys =  0.64 CPU)
   do+local:  1 wallclock secs ( 0.08 usr +  0.55 sys =  0.63 CPU)
      local:  1 wallclock secs ( 0.17 usr +  0.49 sys =  0.66 CPU)
       read:  2 wallclock secs ( 0.10 usr +  0.75 sys =  0.85 CPU)

for a large file (100 iterations) I get:

Size of test.txt: 210013
Benchmark: timing 100 iterations of buf_read, do+local, local, read...
   buf_read: 51 wallclock secs (30.79 usr + 17.34 sys = 48.14 CPU)
   do+local:  1 wallclock secs ( 0.64 usr +  0.32 sys =  0.96 CPU)
      local:  1 wallclock secs ( 0.51 usr +  0.25 sys =  0.76 CPU)
       read:  1 wallclock secs ( 0.41 usr +  0.23 sys =  0.64 CPU)


I'd stick with local, or read with a -s.

At 11:32 PM 3/15/2000 +0000, Fergal Daly wrote:
>At 21:26 15/03/00, Randal L. Schwartz wrote:
>> >>>>> "Nathan" == Nathan Torkington <gnat@frii.com> writes:
>>
>>Nathan> Jeff Pinyan writes:
>> >> read(FILE, $file, -s FILE);
>>
>>Nathan> Oooh, very cute.  So does this work on Windows?  -s returns the 
>>number
>>Nathan> of bytes in the file, but does read() want a character count or a 
>>byte
>>Nathan> count?
>>
>>You don't even need the size.
>>
>>   read FILE, $file, 1e20
>>
>>Adjust the upper bound to suit.  It can't read more than it is. :)
>
>Sorry about this but I get
>
>fergal@zeus:~> perl -e 'open(FILE, "conc");read(FILE, $file, 1e8);print $file'
>
>works fine
>
>fergal@zeus:~> perl -e 'open(FILE, "conc");read(FILE, $file, 1e9);print $file'
>Out of memory!
>
>fergal@zeus:~> perl -e 'open(FILE, "conc");read(FILE, $file, 1e10);print 
>$file'
>Negative length at -e line 1.
>
>
>and file conc is only 748 bytes. It looks like it tries to allocate a 
>buffer big enough before doing it,
>
>Fergal
>
>
>
>==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
>==== Send email to <fwp-request@technofile.org> with message _body_
>====   unsubscribe


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