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/; } } #### 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 () { chomp; tr/A-Z/a-z/; @map_line = split (/\t/); $mapper{$map_line[0]} = $map_line[1]; } close(MAP); my $sub = <<'EOS'; sub { while () { 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.new"); $sub->(); close(IN); clse(OUT);