The chief cause of your program wasting time is that it recompiles every pattern for every replacement. It's better to do it just once, before processing commences. The correct abstraction for a substitution is a code ref (an "anonymous sub"). You pre-compile all your subs, then blast each string through them all.

UPDATE: Fixed stupid bugs in code. ``Engage brain before posting on the Internet!''

This code compares your code (fixed to use the clearer, better form push) with 2 ways of precompiling:

#!/usr/local/bin/perl # Benchmark how fast we can do data-driven s///s use strict; use warnings; use Benchmark; my @pairs = qw/this=that foo=bar baz=quux 2+2=5/; my @names = split /\n/, 'this is not foo 2+2=4 baz(2+2) equals 5 the quick brown fox jumps over the lazy dog '; sub old { my $repeat = shift; my @newrecords; for(1..$repeat) { foreach my $name (@names) { for my $pair (@pairs) { my ($key,$value) = split (/=/, $pair); chomp ($value); $name =~s /\b\Q$key\E\b/$value/ig; # @newrecords = (@newrecords,"$name"); # too slow --ariel +s } push @newrecords, $name; } } @newrecords; } sub new { my $repeat = shift; # setup my @replacer = map { my ($key,$value) = split (/=/, $_); chomp($value); eval <<"END"; sub { \$_[0] =~ s/\\b\\Q$key\\E\\b/$value/ig; } END } @pairs; my @newrecords; for my $i (1..$repeat) { for my $name (@names) { for my $fn (@replacer) { $fn->($name); } push @newrecords, $name; } } @newrecords; } sub new2 { my $repeat = shift; # setup my @replacer = map { my ($key,$value) = split (/=/, $_); chomp($value); sub { $_[0] =~ s/\b$key\b/$value/igo } } @pairs; my @newrecords; for my $i (1..$repeat) { for my $name (@names) { for my $fn (@replacer) { $fn->($name); } push @newrecords, $name; } } @newrecords; } my (@old, @new, @new2); timethese(1, { old => '@old = old(100_000)', new => '@new = new(100_000)', new2 => '@new2 = new2(100_000)', }); print "Done\n"; # Ensure same results @old = old(10); @new = new(10); @new2 = new2(10); (@old == @new and ! grep { $old[$_] ne $new[$_] } 0..$#old) or die "Different:\nOLD: @old\nNEW: @new\n"; (@old == @new2 and ! grep { $old[$_] ne $new2[$_] } 0..$#old) or die "Different:\nOLD: @old\nNEW2: @new2\n";
We only benchmark a single iteration, because <samp>new</samp> and <samp>new2</samp> should only build their subroutines once. In your real program, you'd build the array <samp>@replacer</samp> once, then re-use it for every record.

Also note that <samp>\Q...\E</samp> doesn't speed up the substitutions.

On one computer, the benchmark gives:

Benchmark: timing 1 iterations of new, new2, old...
       new:  9 wallclock secs ( 8.44 usr +  0.14 sys =  8.58 CPU) @  0.12/s (n=1)
            (warning: too few iterations for a reliable count)
      new2:  8 wallclock secs ( 7.97 usr +  0.07 sys =  8.04 CPU) @  0.12/s (n=1)
            (warning: too few iterations for a reliable count)
       old: 30 wallclock secs (30.46 usr +  0.09 sys = 30.55 CPU) @  0.03/s (n=1)
            (warning: too few iterations for a reliable count)
Done
If you read the above code before, please note again this UPDATE: Fixed stupid bugs in code. ``Engage brain before posting on the Internet!''

In reply to Re: Simply Too Slow Find and Replace by ariels
in thread Simply Too Slow Find and Replace by guopan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.