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

Re^2: difference in regex

by haukex (Archbishop)
on May 30, 2018 at 13:32 UTC ( [id://1215457]=note: print w/replies, xml ) Need Help??


in reply to Re: difference in regex
in thread difference in regex

I'm all for TIMTOWTDI, but I wondered about performance. And it turns out that while a split version is faster for short strings, performance suffers a lot the more commas there are in the string:

use warnings; use strict; use Benchmark qw/cmpthese/; my $str = join ',', 'a'..'z', 5; my $exp_path = join ',', 'a'..'z'; my $exp_value = 5; cmpthese(-2, { split => sub { my @x = split /,/, $str; my ($path, $value); $value = pop @x if @x>1; $path = join ',', @x; #die unless $path eq $exp_path && $value eq $exp_value; }, regex => sub { my ($path, $value) = $str=~/^ (?| ([^,]*) | (.*) , ([^,]*) ) $/x; #die unless $path eq $exp_path && $value eq $exp_value; }, } ); __END__ Rate split regex split 326127/s -- -66% regex 966896/s 196% --

Update: This version using rindex beats both split and the regex.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-03-29 07:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found