Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Regexp Legibility

by patrickhaller (Initiate)
on Jun 14, 2009 at 05:21 UTC ( #771344=note: print w/replies, xml ) Need Help??

in reply to Regexp do's and don'ts

I used to (before use strict) compose regexps by hiding the components inside an if, e.g.:
$complex_re = /^($ip) ($host) ($msg)$/ if ( $ip = /\d+\.\d+\.\d+\.\d+/, $host = /[\-\.\w]+/, $msg = /.*/ );
Nowadays, I use strict and eval, e.g.
my $complex_re = eval { my $ip = qr/\d+\.\d+\.\d+\.\d+/; my $host = qr/[\-\.\w]+/; my $msg = qr/.*/; return /^($ip) ($host) ($msg)$/; );
We pay about a 5% penalty for the eval when we use this in a tight loop, however we can solve that by moving the regexp creation outside the loop.
Rate with eval without eval with eval 116279/s -- -5% without eval 121951/s 5% --

Replies are listed 'Best First'.
Re: Regexp Legibility
by ambrus (Abbot) on Jun 14, 2009 at 14:44 UTC

    I think you should use do { instead of eval { there.

      Looks like eval runs faster...
      Rate as usual with do as usual 63091/s -- -29% with do 88496/s 40% -- Rate with eval as usual with eval 59773/s -- -9% as usual 65359/s 9% --

        No, it doesn't look like that.

        In one run "with do" was 40% faster than "as usual", while in the second run, "with eval" iswas 9% slower than "as usual".

        The "Rate" shows you how many times the specific routine could be run per time unit, so in this case, runs per second.

        What makes you think, that the eval-solution runs faster? ;)

        BTW, you can do comparisons with more than two subroutines. So you can do one run and compare all three (or more) routines.

        Please show your benchmark code

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://771344]
and John Coltrane plays...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2018-05-23 21:27 GMT
Find Nodes?
    Voting Booth?