Well then, it's something like below that might fit.

#!/usr/bin/perl use strict; use warnings; use Benchmark; my %entities = (); my $counter = 10000; while ($counter-- > 0) { $entities{ '&ent' . sprintf( '%05d', $counter ) . ';' } = $counter; } my $text_to_be_changed = <<EOT; This is some text containing five (&ent00005;) entities that will be changed: &ent00029;, &ent00129;, &ent00229;, &ent00329; and &ent00429; The pseudo_entities below should rest unchanged: \&dent00029;, \&dont_change;, \& qwerty ;, 12345;. EOT timethese( 1000, { 'with_splitting' => \&with_splitting, 'regexish' => \&regexish, }); exit; sub with_splitting { my @modified_parts = (); my @split_on_semicolon = split( /;/, $text_to_be_changed); foreach my $ending_in_semicolon (@split_on_semicolon) { if ( $ending_in_semicolon =~ m/(&\w+)$/ and exists( $entities{ "$1;"} ) ) { $ending_in_semicolon =~ s/(&\w+)$/$entities{ "$1;" }/; } else { $ending_in_semicolon .= ';' ; } push( @modified_parts, $ending_in_semicolon ); } my $result = join( '', @modified_parts ); #print "RESULT: \n", $result; } sub regexish { my $huge_entity = join '|', keys %entities; $text_to_be_changed =~ s/($huge_entity)/$entities{$1}/g; #print "RES2: \n", $text_to_be_changed; }
The results are interesting:
Benchmark: timing 1000 iterations of regexish, with_splitting... regexish: 59 wallclock secs (58.16 usr + 0.02 sys = 58.18 CPU) @ 17 +.19/s (n=1000) with_splitting: 0 wallclock secs ( 0.02 usr + 0.00 sys = 0.02 CPU) +@ 50000.00/s (n=1000) (warning: too few iterations for a reliable count)

In reply to Re^3: Improve processing time for string substitutions by Krambambuli
in thread Improve processing time for string substitutions by valavanp

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.