Difference between Perl backticks and pipe

Viewed 102

I have an executable tool which return data separated by tabs as output , the data is very huge ,and want the output of the tool be an input for Perl program for further processing , what is the preferred way from performance perspective to use

@res=`mytool`

and processing @res

or use the pipe operator to read the tool while returning the results an process it something like the below :

open(RES, "mytool |") or die "Couldn't fork: $!\n";
while (<RES>) { # ... } 
1 Answers

The second form will almost always be quicker than the first (at least if your code and the code providing the list will take some time to compute).

Here an easy example:

use strict;

use Benchmark qw(:all) ;
timethis (3,\&backtick );
timethis (3,\&pipe);

sub backtick {
    my @res=`locate .ssh`;
    my $count =0;
    foreach my $line (@res) {
           select(undef,undef,undef, .02); #20-millisecond delay
           $count += length($line);
    }
    print "$count \n";
}

sub  pipe{
    local *RES;
    open(RES, "locate .ssh |") or die "Couldn't fork: $!\n";

    my $count =0;
    while (<RES>) { # 
           select(undef,undef,undef, .02); #20-millisecond delay
           $count += length($_);
    }
    print "$count \n";
}

will on my pc print:

4921
4921
4921
timethis 3: 21 wallclock secs ( 0.00 usr  0.00 sys + 15.11 cusr  0.19 csys = 15.                                                                  30 CPU) @  0.20/s (n=3)
            (warning: too few iterations for a reliable count)
4921
4921
4921
timethis 3: 16 wallclock secs ( 0.00 usr  0.00 sys + 15.15 cusr  0.18 csys = 15.                                                                  33 CPU) @  0.20/s (n=3)

(The locate .ssh takes about 5 seconds to finish)

Related