Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by mrguy123 (Hermit)
on Aug 09, 2012 at 11:49 UTC ( #986496=note: print w/ replies, xml ) Need Help??


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

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


Comment on Re^2: Matching simple patterns - is there a faster way?
Download Code
Re^3: Matching simple patterns - is there a faster way?
by flexvault (Parson) on Aug 09, 2012 at 14:15 UTC

    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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (10)
As of 2014-12-28 16:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (182 votes), past polls