#!/usr/bin/perl use strict; use warnings; use Benchmark::Forking qw( timethese cmpthese ); # UnixOS # use Benchmark qw(:all) ; # WindowsOS my @preserved = @ARGV; sub test_grep { @ARGV = @preserved; # restore original @ARGV my @matched = grep { $_ =~ /AGGT/ } <>; if (@matched) { for(@matched) { chomp $_; print "Grep: " . length($_) . "\n";} } close ARGV if eof; } sub test_while { @ARGV = @preserved; # restore original @ARGV my $count; while (<>) { if ($_ =~ /^>hsa/){ chomp (my $line = <>); $count .= length($line); $count .= " "; } } continue { close ARGV if eof; # reset $. each file } print "While: " . $count . "\n"; } my $results = timethese(2, { Grep => \&test_grep, While => \&test_while }, 'none'); cmpthese( $results );