• Download Gist
benchmark common unix utilities agaisnt pure Perl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#!/usr/bin/perl
 
use strict;
use warnings;
 
use Carp;
use List::Util qw/first/;
use Benchmark;
 
sub benchmark {
my $subs = shift;
 
my ($k, $sub) = each %$subs;
my $value = $sub->();
croak "bad" if first { $value ne $_->() and print "$value\n", $_->(), "\n" } values %$subs;
 
Benchmark::cmpthese -1, $subs;
}
 
benchmark {
cat => sub {
return qx/cat $0/;
},
perl => sub {
open my $fh, "<", $0 or die "$!";
local $/ = undef;
return scalar <$fh>;
},
};
 
benchmark {
ls => sub {
return join "", map { chomp; $_ } qx/ls/;
},
perl => sub {
return join "", sort glob "*";
},
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.