Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^3: One line assigment statement with regex match

by kaif (Friar)
on Jun 23, 2005 at 01:59 UTC ( [id://469265]=note: print w/replies, xml ) Need Help??


in reply to Re^2: One line assigment statement with regex match
in thread One line assigment statement with regex match

So, a lot of people like to say that. And indeed, sometimes index is ten times faster. But sometimes it's more than three times slower!

use Benchmark qw(:all); $text = <<EOF; aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +aaaaaaaaaa EOF $pattern = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; cmpthese($count, { 'regex' => sub { $text =~ $pattern }, 'index' => sub { index $text, $pattern }, }); __DATA__ Rate index regex index 630601/s -- -67% regex 1914815/s 204% --

Moreover, increasing the lengths of the text and pattern, I can make the regex be 40 times faster*. See Re^8: "advanced" Perl functions and maintainability for reasons why people use regexes instead of index. Personally, I still don't understand why there even is a difference in speed -- shouldn't the regex engine be optimized to notice that this is a search for a constant string and then call the same function as index?

*: No, I'm not kidding. The output follows. Moreover, for this example, adding a single study $text is an extra 10 times faster, completely obliterating index.

Rate index regex study index 178/s -- -98% -100% regex 7538/s 4124% -- -92% study 98871/s 55311% 1212% --
Update: I'm running perl v5.8.5 built for i686-linux.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-20 03:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found