Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re^2: difference in regex by haukex
in thread difference in regex by ovedpo15

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 admiring the Monastery: (6)
As of 2024-04-18 06:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found