Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • 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 musing on the Monastery: (4)
As of 2024-04-24 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found