Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-25 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found