Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Did the inefficiency of /i get fixed?

by Belgarion (Chaplain)
on May 19, 2004 at 04:09 UTC ( #354516=note: print w/replies, xml ) Need Help??


in reply to Did the inefficiency of /i get fixed?

To amplify what sgifford said: it would appear that the longer the string being matched, the bigger the difference between the two methods. For example, using sgifford's script, but with a $s of one character, the numbers on my machine are:

Rate brackets slash i brackets 719424/s -- -7% slash i 775194/s 8% --

While, when $s is 16KB long, the difference is more pronounced, with the following:

Rate brackets slash i brackets 3508/s -- -66% slash i 10434/s 197% --

Now the interesting question: sgifford's regex is very simple. What happens when a more complicated test is used. When using the following test code:

#!/usr/bin/perl -w use strict; use Benchmark 'cmpthese'; our $s = "HELLOWORLD" x 1024; cmpthese(1_000_000, { 'slash_i' => sub { $s =~ m/hello/i; $s =~ m/jello +/i }, 'brackets' => sub { $s =~ m/[Hh][Ee][Ll][Ll][Oo]/ +; $s =~ m/[Jj][Ee][Ll][Ll][Oo]/ +; }, });

produce the following results:

Rate brackets slash_i brackets 5633/s -- -66% slash_i 16683/s 196% --

It would appear that the length of the string amplifies the difference more than the complexity of the regular expression. (At least when comparing /[Tt]/ with /t/i.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2023-03-27 16:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (65 votes). Check out past polls.

    Notices?