Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowens/155862 to your computer and use it in GitHub Desktop.
Save cowens/155862 to your computer and use it in GitHub Desktop.
#!/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 "*";
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment