Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Matching simple patterns - is there a faster way?

by flexvault (Parson)
on Aug 09, 2012 at 09:43 UTC ( #986465=note: print w/ replies, xml ) Need Help??


in reply to Matching simple patterns - is there a faster way?

mrguy123,

I benchmarked the code replacing the 'print' with '{ }':

Rate index regex regex 1562500/s -- -34% index 2380952/s 52% --
Your not testing print, are you?

What is funny, is that I ran the code 3 times, and the exact same result occurred all 3 times. I usually take the middle one!

"Well done is better than well said." - Benjamin Franklin


Comment on Re: Matching simple patterns - is there a faster way?
Download Code
Re^2: Matching simple patterns - is there a faster way?
by mrguy123 (Hermit) on Aug 09, 2012 at 11:49 UTC
    Hi, please see my benchmark test
    use strict; use Benchmark qw(cmpthese); # A big, sorted list (475254 elements) my @list = ('a'..'zzzz'); ##Put the list into a string my $string = join(/ /, @list); $string .= "\n"; cmpthese(0, { # Looking for a random item in @list with grep index => sub { # A random element in the list my $pattern = 'fqsu fqsv fqsw fqsx fqsy'; my $result = index($string, $pattern); }, # Looking for a random item in @list linearly (with foreach) regex => sub { my $pattern = 'fqsu fqsv fqsw fqsx fqsy'; $string =~ /$pattern/; }, } );
    I also did another test where I read a proper HTML file and received similar results. Can you please tell me what you did different?


    Everybody seems to think I'm lazy
    I don't mind, I think they're crazy

      mrguy123,

      As requested ( I typed this in from the code ):

      sub index { if ( index('yabba dabba doo','abba' ) >= 0 ) { } } sub regex { $_ = 'yabba dabba doo'; if ( /abba/ ) { } } cmpthese ( 500_000, { regex => sub { &regex }, index => sub { &index }, }, );
      I have found 'index' usually faster than 'regex' in this type of situation, but 'regex' has more power when needed.

      Regards...Ed

      "Well done is better than well said." - Benjamin Franklin

Re^2: Matching simple patterns - is there a faster way?
by mrguy123 (Hermit) on Aug 09, 2012 at 12:15 UTC
    Hi flexvault, I am assuming you did the benchmark on the 'yabba dabba doo' example, which makes perfect sense because that is what I posted.
    However, the real data is much larger and that may cause the difference in the benchmark
    You can see my code example below for regex/index matching with larger amount of data

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-12-29 08:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (185 votes), past polls