#!/usr/bin/perl # use Benchmark qw/ cmpthese /; open F, "conf/English/stops" or die "Pb : $!\n"; chomp(@stops = ); open G, "test/texte" or die "Pb : $!\n"; chomp(@text = ); for $line (@text) { push @data, split /\s+/, $line } %seen = map { $_ => 1 } @stops; my @regexens = map { qr/\Q$_\E/ } @stops; { local $" = '|'; $r = qr/@regexens/; } cmpthese(100, { 'string' => sub { WORD: foreach my $word (@data) { foreach my $stop (@stops) { next WORD if ($stop eq $word); } push @words, $word; } }, 'regex' => sub { WORD2: foreach my $word (@data) { next WORD2 if ($word =~ m/^$r$/); push @words, $word; } }, 'hash' => sub { WORD: foreach my $word (@data) { next WORD if $seen{$word}; push @words, $word; } } }); __DATA__ (warning: too few iterations for a reliable count) Rate string regex hash string 30.0/s -- -70% -99% regex 99.9/s 233% -- -97% hash 3704/s 12230% 3607% --