in reply to Re^2: How to speed up multiple regex in a loop for a big data?
in thread How to speed up multiple regex in a loop for a big data?
outputs...use Benchmark 'cmpthese'; my $a = 'foo bar doz' x 100; $a .= ' hello '.$a; my $sub = sub { /\bhello\b/; /\bhello\b/; /\bhello\b/; /\bhello\b/; /\bhello\b/; /\bhello\b/; /\bhello\b/; /\bhello\b/; }; cmpthese(-3, { loop => sub { for (($a) x 10) { for my $i (1..8) { /\bhello\b/; } } }, sub => sub { for (($a) x 10) { $sub->() } }, inline => sub { for (($a) x 10) { /\bhello\b/; /\bhello\b/; /\bhello\b/; /\bhello\b/; /\bhello\b/; /\bhello\b/; /\bhello\b/; /\bhello\b/; } }
and anyway, it's easy to modify my code to remove the subroutine call from the loop just moving the loop inside the sub:Rate loop sub inline loop 4157/s -- -5% -16% sub 4363/s 5% -- -12% inline 4943/s 19% 13% --
open(MAP, "<$new_name_map_file"); while (<MAP>) { chomp; tr/A-Z/a-z/; @map_line = split (/\t/); $mapper{$map_line[0]} = $map_line[1]; } close(MAP); my $sub = <<'EOS'; sub { while (<IN>) { print "%"; tr/A-Z/a-z/; EOS for my $name (sort keys %mapper) { my $qname = quotemeta $name; my $qrepl = quotemeta $mapper{$name}; $sub .= "s{\b$qname\b}{$qrepl}g;\n"; } $sub .= <<'EOS' print OUT $_; } } EOS $sub = eval $sub; die if $@; open(IN, "<input_file"); open(OUT, ">input_file.new"); $sub->(); close(IN); clse(OUT);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: How to speed up multiple regex in a loop for a big data?
by MonkInPleasanton (Initiate) on May 25, 2006 at 16:45 UTC | |
by salva (Canon) on May 25, 2006 at 16:58 UTC | |
|
Re^4: How to speed up multiple regex in a loop for a big data?
by ruzam (Curate) on May 25, 2006 at 17:10 UTC |