Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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% --


Comment on Re: Optimizing a regex that replaces near the end of a string
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (13)
As of 2014-10-20 08:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (74 votes), past polls