#!/usr/bin/perl
#
use Benchmark qw/ cmpthese /;
open F, "conf/English/stops" or die "Pb : $!\n";
chomp(@stops = <F>);
open G, "test/texte" or die "Pb : $!\n";
chomp(@text = <G>);
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% --
package Letter;
use overload
'+' => \&do_plus,
'-' => \&do_minus,
'""' => \&do_print;
sub new {
my ($ref, $letter) = @_;
bless [ $letter ], $ref;
}
sub do_plus {
my $self = shift;
my $plus = shift;
$self->[0] = chr(ord($self->[0]) + 1);
}
sub do_minus {
my $self = shift;
my $plus = shift;
$self->[0] = chr(ord($self->[0]) - 1);
}
sub do_print {
my $self = shift;
$self->[0];
}
package main;
my $l = new Letter("F");
$l--;
print $l;
Funny :
$#a = 0 or $#a = -1 autovivifies @a
$#a >>= 1 does not
-1 >>= 1 gives, on my computer 2147483647
$#a = 2147483647 does not either
|