#!/usr/bin/perl use strict; use warnings; use Benchmark::Forking qw( timethese cmpthese ); # UnixOS # use Benchmark qw(:all) ; # WindowsOS sub test_grep { my @matched = grep { $_ =~ /AGGT/ } <>; if (@matched) { for(@matched) { chomp $_; print "Grep: " . length($_) . "\n";} } close ARGV if eof; } sub test_while { my $count; while (<>) { if ($_ =~ /^>hsa/){ chomp (my $line = <>); $count .= length($line); $count .= " "; } } continue { close ARGV if eof; # reset $. each file } print "While: " . $count . "\n"; } # test_grep(); # Works fine separate but not combined test_while(); # Works fine separate but not combined __DATA__ $ perl test.pl test.txt Grep: 107 Grep: 251 ^C $ perl test.pl test.txt While: 107 251 ^C __END__ my $results = timethese( 2, { 'Grep' => sub { }, 'While' => sub { }, }, 'none' ); cmpthese( $results );