Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Simply Too Slow Find and Replace

by ariels (Curate)
on Jun 03, 2002 at 06:43 UTC ( #171149=note: print w/ replies, xml ) Need Help??


in reply to Simply Too Slow Find and Replace

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!''


Comment on Re: Simply Too Slow Find and Replace
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://171149]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2014-10-23 00:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (122 votes), past polls