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

Re: Optimizing a regex that replaces near the end of a string

by thunders (Priest)
on Mar 18, 2009 at 21:31 UTC ( #751578=note: print w/replies, xml ) Need Help??

in reply to Optimizing a regex that replaces near the end of a string

If your end string is really something static like that, you may not want to use a regex at all. Here's a case insensitive replacement using rindex and substr:
use Benchmark qw(cmpthese); fbard@devo1:~/spot_camp_layout$ cat rindex use Benchmark qw(cmpthese); my $x = ("x" x 100_000)."</body></html>"; my $lc_x = lc($x); my $end_html = qr{(</body></html>)}; cmpthese(100_000,{ rindex_substr => sub{ my $str = $x; substr($str,rindex($lc_x,"</body>"),0,"yyyy"); }, regex => sub{ my $str = $x; $str =~ s|$end_html$|yyyy$1|i; } }); Rate regex rindex_substr regex 18416/s -- -75% rindex_substr 73529/s 299% --

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://751578]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2018-01-19 22:05 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (223 votes). Check out past polls.