Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: better (faster) way of writing regexp

by Marshall (Prior)
on Dec 03, 2009 at 07:11 UTC ( #810761=note: print w/ replies, xml ) Need Help??


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

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% --


Comment on Re^3: better (faster) way of writing regexp
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2014-12-22 05:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (110 votes), past polls