Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Regexes are slow (or, why I advocate String::Index)

by japhy (Canon)
on May 14, 2004 at 21:44 UTC ( [id://353505]=perlmeditation: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    $str =~ s/X[^X]*$/X/;
    
  2. or download this
    $str =~ s/(.*X).*/$1/;
    
  3. or download this
    $str =~ s/.*X\K.*//;
    
  4. or download this
    $str =~ /.*X/g and $str =~ s/\G.*//;
    
  5. or download this
    ($str = reverse $str) =~ s/^[^X]+//;
    $str = reverse $str;
    
  6. or download this
    substr($str, rindex($str, "X")+1) = "";
    
  7. or download this
    use Benchmark 'cmpthese';
    my $str = "alphabet X alphabet" x 100 . "junk at the end" x 10;
    ...
    capt_repl  41274/s      545%        4%        --      -35%      -82%
    rx_rx      63380/s      891%       60%       54%        --      -73%
    substr    230796/s     3507%      482%      459%      264%        --
    
  8. or download this
    cmpthese(-5, {
      last => sub { my $x = $str; $x =~ s/([A-Z])[^A-Z]*$/$1/ },
    ...
    capt_repl 30599/s      447%        --      -21%      -24%
    rx_rx     38948/s      597%       27%        --       -3%
    sexeger   40049/s      616%       31%        3%        --
    
  9. or download this
    /* C code */
    printf("%s", strpbrk("breakfast", "aeiou"));
    /* prints: eakfast */
    
  10. or download this
    if ("breakfast" =~ /[aeiou]/) {
      $x = substr("breakfast", $-[0]);
    }
    
  11. or download this
    # crindex is char-class-based rindex()
    use String::Index 'crindex';
    ...
    rx_rx     38194/s      584%       25%        --       -4%      -30%
    sexeger   39594/s      609%       29%        4%        --      -28%
    crindex   54829/s      882%       79%       44%       38%        --
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://353505]
Approved by Corion
Front-paged by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-19 15:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found