in reply to Re: better (faster) way of writing regexp
in thread better (faster) way of writing regexp
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: better (faster) way of writing regexp
by ikegami (Patriarch) on Dec 02, 2009 at 18:07 UTC | |
by JavaFan (Canon) on Dec 02, 2009 at 18:19 UTC | |
by ikegami (Patriarch) on Dec 02, 2009 at 18:33 UTC | |
by Marshall (Canon) on Dec 03, 2009 at 07:52 UTC | |
by ikegami (Patriarch) on Dec 03, 2009 at 17:42 UTC | |
by Marshall (Canon) on Dec 03, 2009 at 22:28 UTC | |
by ikegami (Patriarch) on Dec 03, 2009 at 22:38 UTC | |
Re^3: better (faster) way of writing regexp
by Marshall (Canon) on Dec 03, 2009 at 07:11 UTC |
In Section
Seekers of Perl Wisdom