Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
If we are assuming that isook() is fine functionally, there is a much faster way. Don't use regex! use substr! I ran same benchmarks along with a new one, substr(). Code is below. My machine, (older Win XP running Perl 5.10) is considerably slower than the other machines posting results, but I would expect similar results in the ranking if you run the code below. Having said that, all these solutions run so fast that it is hard to see how this is going to make much difference, but that depends upon the application!

This substr idea is also faster even with some simple format error tests like this snippet... This $1,$2,$3 stuff is "expensive".

substr => sub{ my $t2 = '20090123'; my $year =substr ($t2,0,4); my $mon = substr ($t2,4,2); my $day = substr ($t2,6,2); die if (length($t2)!= 8); die if ($t2 =~ /\D/); },
#!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); }, substr => sub{ my $t2 = '20090123'; my $year =substr ($t2,0,4); my $mon = substr ($t2,4,2); my $day = substr ($t2,6,2); }, 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__ output: Benchmark: timing 1000000 iterations of chkunpk, dirunpk, isook, range +, repeat, substr... chkunpk: 4 wallclock secs ( 5.05 usr + 0.00 sys = 5.05 CPU) @ 198137.51/s (n=1000000) dirunpk: 2 wallclock secs ( 3.31 usr + 0.00 sys = 3.31 CPU) @ 301841.23/s (n=1000000) isook: 3 wallclock secs ( 3.08 usr + 0.00 sys = 3.08 CPU) @ 324886.29/s (n=1000000) range: 4 wallclock secs ( 3.23 usr + 0.00 sys = 3.23 CPU) @ 309214.59/s (n=1000000) repeat: 4 wallclock secs ( 3.03 usr + 0.00 sys = 3.03 CPU) @ 329924.12/s (n=1000000) substr: 2 wallclock secs ( 1.22 usr + 0.00 sys = 1.22 CPU) @ 820344.54/s (n=1000000) Rate chkunpk dirunpk range isook repeat substr chkunpk 198138/s -- -34% -36% -39% -40% -76% dirunpk 301841/s 52% -- -2% -7% -9% -63% range 309215/s 56% 2% -- -5% -6% -62% isook 324886/s 64% 8% 5% -- -2% -60% repeat 329924/s 67% 9% 7% 2% -- -60% substr 820345/s 314% 172% 165% 153% 149% --

In reply to Re^3: better (faster) way of writing regexp by Marshall
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 contemplating the Monastery: (6)
    As of 2014-11-27 01:58 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (178 votes), past polls