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

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I'd expect that the unpack is the faster method to split the fields, but after inserting the following code in the above benchmark, in the average "dirunpk" (direct unpack) took the same amount of time than the "repeat" test.

#!perl use v5.10; use strict; use warnings; use Benchmark qw(:all); my $results = timethese( 1e6, { repeat => sub{ my $t1 = '20090123'; $t1 =~ /(\d\d\d\d)(\d\d)(\d\d)/; my ($y1,$m1,$d1) = ($1,$2,$3); }, range => sub{ my $t2 = '20090123'; $t2 =~ /(\d{4})(\d{2})(\d{2})/; my ($y2,$m2,$d2) = ($1,$2,$3); }, chkunpk => sub{ my $t3 = '20090123'; $t3 =~ m/([0-9]{8})/; my ($y3,$m3,$d3) = unpack "A4 A2 A2", $1; }, dirunpk => sub{ my $t3 = '20090123'; my ($y4,$m4,$d4) = unpack "A4 A2 A2", $t3; }, isook => sub{ my $t5 = '20090123'; $t5 =~ /(....)(..)(..)/; my ($y5,$m5,$d5) = ($1,$2,$3); }, } ); cmpthese( $results ) ; __END__ 1st run: Benchmark: timing 1000000 iterations of chkunpk, dirunpk, isook, range +, repeat... chkunpk: 4 wallclock secs ( 3.11 usr + 0.00 sys = 3.11 CPU) @ 32 +1646.83/s (n=1000000) dirunpk: 2 wallclock secs ( 2.06 usr + 0.00 sys = 2.06 CPU) @ 48 +4966.05/s (n=1000000) isook: 1 wallclock secs ( 1.95 usr + 0.00 sys = 1.95 CPU) @ 51 +2032.77/s (n=1000000) range: 3 wallclock secs ( 2.16 usr + 0.00 sys = 2.16 CPU) @ 46 +3821.89/s (n=1000000) repeat: 2 wallclock secs ( 1.97 usr + 0.00 sys = 1.97 CPU) @ 50 +8130.08/s (n=1000000) Rate chkunpk range dirunpk repeat isook chkunpk 321647/s -- -31% -34% -37% -37% range 463822/s 44% -- -4% -9% -9% dirunpk 484966/s 51% 5% -- -5% -5% repeat 508130/s 58% 10% 5% -- -1% isook 512033/s 59% 10% 6% 1% -- 2nd run: Benchmark: timing 1000000 iterations of chkunpk, dirunpk, isook, range +, repeat... chkunpk: 2 wallclock secs ( 3.11 usr + 0.00 sys = 3.11 CPU) @ 32 +1646.83/s (n=1000000) dirunpk: 3 wallclock secs ( 2.05 usr + 0.00 sys = 2.05 CPU) @ 48 +8519.79/s (n=1000000) isook: 2 wallclock secs ( 1.98 usr + 0.00 sys = 1.98 CPU) @ 50 +4032.26/s (n=1000000) range: 1 wallclock secs ( 2.11 usr + 0.00 sys = 2.11 CPU) @ 47 +4158.37/s (n=1000000) repeat: 3 wallclock secs ( 2.06 usr + 0.00 sys = 2.06 CPU) @ 48 +4966.05/s (n=1000000) Rate chkunpk range repeat dirunpk isook chkunpk 321647/s -- -32% -34% -34% -36% range 474158/s 47% -- -2% -3% -6% repeat 484966/s 51% 2% -- -1% -4% dirunpk 488520/s 52% 3% 1% -- -3% isook 504032/s 57% 6% 4% 3% --

The faster way seems to be using the capture made of dots as in "isook", as the OP said that the string IS a date in ISO format.

UPDATE: Name of tests changed (for readability) and comparison table added. Results are for two consecutive runs.


In reply to Re^2: better (faster) way of writing regexp by vitoco
in thread better (faster) way of writing regexp by jodaka

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (10)
    As of 2014-12-29 16:43 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (193 votes), past polls