I had the same thought, but unless I am doing something dumb (quite likely:), then strangely it seems that unpack is slower than even the explicit regex?
#! perl -slw use strict; use Benchmark qw[ cmpthese ]; our @data = map{ join' ', '2004-05-13', '14:02:00', ('blah') x (1+rand( 9 )) } 1 .. 1000; cmpthese( -1, { greedy => q[ my( $date, $time, $text ); m[(^\S*)\s(\S*)\s(.*$)] and ( $date, $time, $text ) = ( $1, $2, $3 ) # and print "greedy: $date|$time|$text" for @data; ], explicit => q[ my( $date, $time, $text ); m[(^\d{4}\-\d{2}\-\d{2})\s(\d{2}:\d{2}:\d{2})\s(.*$)] and ( $date, $time, $text ) = ( $1, $2, $3 ) # and print "explicit: $date|$time|$text" for @data; ], unpack => q[ my( $date, $time, $text ); ( $date, $time, $text ) = unpack 'A10 x A8 x A*', $_ # and print "unpack: $date|$time|$text" for @data; ], }); __END__ P:\test>362106 Rate unpack explicit greedy unpack 158/s -- -41% -53% explicit 267/s 70% -- -21% greedy 338/s 114% 26% --
What stupidity am I guilty of?
In reply to Re^2: fast greedy regex
by BrowserUk
in thread fast greedy regex
by js1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |