Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Character class in an array

by RMGir (Prior)
on Sep 17, 2002 at 17:37 UTC ( [id://198571]=note: print w/replies, xml ) Need Help??


in reply to Character class in an array

Others have explained that $" is the source of your problem.

However, there are other problems with what you're doing.

You're repeating that interpolation on every trip through the loop, when you could be using qr// to compile your regex once and then forget about it.

Here are some comparisons of a few of the other solutions mentioned in this thread, along with one where the regex is pre-built with qr//. These results are with 5.6.1 on cygwin; YMMV.

$ perl testLocal.pl Benchmark: running localWhile, localWhileQR, whileLocal, each for at l +east 3 CPU seconds... localWhile: 4 wallclock secs ( 3.02 usr + 0.00 sys = 0.02 CPU) @ 28 +894.20/s (n=87116) localWhileQR: 4 wallclock secs ( 3.17 usr + 0.01 sys = 3.18 CPU) @ +45765.70/s (n=145718) whileLocal: 4 wallclock secs ( 3.09 usr + 0.00 sys = 3.09 CPU) @ 20 +884.62/s (n=64617) Rate whileLocal localWhile localWhileQR whileLocal 20885/s -- -28% -54% localWhile 28894/s 38% -- -37% localWhileQR 45766/s 119% 58% --
The benchmark code is here:
$ cat testLocal.pl #!/usr/bin/perl -w use strict; use Benchmark qw(cmpthese); my @a=qw(a x z); # if you increase the number of test strings, the differences # get even more pronounced... my @strings=qw(a x z vvvvv vvvvva vvvvvb vvvvvx vvvvvz aaa axz azz axx +); sub localWhile{ local $"=""; my $count; foreach(@strings) { $count++ if /^[@a]{3}$/; } die unless $count==4; } sub whileLocal{ my $count; foreach(@strings) { local $"=""; $count++ if /^[@a]{3}$/; } die unless $count==4; } sub localWhileQR{ my $count; local $"=""; my $re=qr/^[@a]{3}$/; foreach(@strings) { $count++ if /$re/; } die unless $count==4; } cmpthese(-3, { localWhile => \&localWhile, localWhileQR => \&localWhileQR, whileLocal => \&whileLocal } );

--
Mike

Replies are listed 'Best First'.
Re: Re: Character class in an array
by Fastolfe (Vicar) on Sep 17, 2002 at 22:05 UTC
    You're repeating that interpolation on every trip through the loop, when you could be using qr// to compile your regex once and then forget about it.

    The /o flag on the regex will also work without requiring him to create a regex scalar with qr//.

      And it may not work always. See 'o' modifier clarification needed

      ____________________________________________________
      ** The Third rule of perl club is a statement of fact: pod is sexy.

        The original poster had a constant array and the post I was responding to was discussing the benefits of optimizing queries for "once-only" compilation. Obviously if you are wanting to change the array between pattern matches, you'll want to ensure your pattern is current.

        Sorry, misreplied.

      But will fail if the regex is used multiple times with varying array contents.

      Makeshifts last the longest.

        I was just extending the original poster's qr/// suggestion. If you interpolate a variable in a 'qr' string, it "stays" interpolated and will not change if the value of the original variable changes with it, unless you re-create the string for every iteration. Obviously, if you're going to want to use qr/// or the /o flag on a regex, you are doing so because you wish Perl to compile the regular expression once (at least within the lexical scope of the qr/// string).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-12 18:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found