Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
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

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
Node Status?
node history
Node Type: note [id://986496]
help
Chatterbox?
[marioroy]: why do some posters not know to provide a good SSCCE? Nested loops?
[Corion]: marioroy: Producing good SSCCE is an acquired skill :) You have to produce bad SSCCEs for a long time until you get good. And when you get good, you don't need them that much anymore because you simply isolate the problem and then solve it ;)
[marioroy]: ... and kcott is able to provide something. Amazing.

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2017-08-18 08:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (297 votes). Check out past polls.

    Notices?