Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by mrguy123 (Hermit)
on Aug 09, 2012 at 11:49 UTC ( [id://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

Replies are listed 'Best First'.
Re^3: Matching simple patterns - is there a faster way?
by flexvault (Monsignor) 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://986496]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-03-28 23:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found