in reply to Re: Fast file parsing
in thread Fast file parsing
It's faster to use index() when looking for exact matches.
That ain't necessarily so, particularly when anchored: /^%%/ only needs to check the start of the string, whereas index($_, '%%') needs to scan to the first match, possibly through the entire string.
Update - of course using substr and eq is a much more obvious way to check this, and I've added some options to the code below to reflect that./Update
Try this:
#!/usr/bin/perl -w use Benchmark qw/ cmpthese /; my $count = shift; our $a = '%%'; our $b = ' ' x 10000; cmpthese($count, { first_re => q{ $match = "$a$b" =~ /^%%/ }, last_re => q{ $match = "$b$a" =~ /^%%/ }, miss_re => q{ $match = "$b" =~ /^%%/ }, first_index => q{ $match = index("$a$b", "%%") == 0}, last_index => q{ $match = index("$b$a", "%%") == 0}, miss_index => q{ $match = index("$b" , "%%") == 0}, first_substr => q{ $match = substr("$a$b", 0, 2) eq '%%' }, last_substr => q{ $match = substr("$b$a", 0, 2) eq '%%' }, miss_substr => q{ $match = substr("$b" , 0, 2) eq '%%' }, })
Hugo
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Fast file parsing
by perrin (Chancellor) on Mar 09, 2004 at 18:15 UTC |