develooper Front page | perl.perl5.porters | Postings from April 2011

Why does $/ have influence on sysread?

Thread Next
From:
H.Merijn Brand
Date:
April 28, 2011 04:39
Subject:
Why does $/ have influence on sysread?
Message ID:
20110428133856.488fc3f6@pc09.procura.nl
$ bleadperl test.pl
            Rate with_rs without
with_rs 180422/s      --    -34%
without 274368/s     52%      --
$

--8<--- test.pl
use strict;
use warnings;

use Benchmark qw( cmpthese );

my $data = "x" x 8_000_000;
my $rec  = " " x 512;

sub foo
{
    my $n = 0;
    open my $fh, "<", \$data;
    while (sysread $fh, $rec, 256, 0) {
        $n++;
        }
    $n;
    } # foo

sub bar
{
    my $n = 0;
    local $/ = \256; # <-- only addition!
    open my $fh, "<", \$data;
    while (sysread $fh, $rec, 256, 0) {
        $n++;
        }
    $n;
    } # foo

cmpthese (-2, {
    without => \&foo,
    with_rs => \&bar,
    });
-->8---

-- 
H.Merijn Brand  http://tux.nl      Perl Monger  http://amsterdam.pm.org/
using 5.00307 through 5.12 and porting perl5.13.x on HP-UX 10.20, 11.00,
11.11, 11.23 and 11.31, OpenSuSE 10.1, 11.0 .. 11.3 and AIX 5.2 and 5.3.
http://mirrors.develooper.com/hpux/           http://www.test-smoke.org/
http://qa.perl.org      http://www.goldmark.org/jeff/stupid-disclaimers/

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About