On Sat, Jun 5, 2010 at 09:59, Dr.Ruud <rvtol+use...@isolution.nl> wrote:
> Chas. Owens wrote:
>
>> my $xml = do {
>>  open my $fh, "<", $filename
>>  or die "could not open $filename: $!";
>>  local $/;
>>  <$fh>;
>> };
>
> I would write that as
>
>  my $xml;
>  {   open my $fh, "<", $filename
>        or die "could not open $filename: $!";
>      local $/;
>      $xml= <$fh>;
>  }
>
> because it puts the file contents in application memory only once.
snip

If you are worried about the size of the file, you shouldn't be using
either method.  As for the do vs the block, it makes next to no
difference in speed:

Perl version 5.012000

bench.pl (419 bytes):
         Rate    do block
do    28444/s    --   -1%
block 28768/s    1%    --

/usr/share/dict/words (2486813 bytes):
         Rate    do block
do    28709/s    --   -1%
block 28980/s    1%    --

#!/usr/bin/perl

use strict;
use warnings;

use Benchmark;

print "Perl version $]\n";

my $s = $0;

my %subs = (
        do => sub {
                my $script = do {
                        open my $fh, "<", $s;
                        local $/;
                        <$fh>;
                };
        },
        block => sub {
                my $script;
                {
                        open my $fh, "<", $s;
                        local $/;
                        $script = <$fh>;
                }
        }
);

for $s ($0, "/usr/share/dict/words") {
        print "\n$s (", -s $s, " bytes):\n";
        Benchmark::cmpthese -1, \%subs;
}



-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to