[32804] in Perl-Users-Digest
Perl-Users Digest, Issue: 4068 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Nov 1 14:09:29 2013
Date: Fri, 1 Nov 2013 11:09:02 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 1 Nov 2013 Volume: 11 Number: 4068
Today's topics:
Re: Can this be combined into one statement? <jblack@nospam.com>
Re: Can this be combined into one statement? <ben@morrow.me.uk>
Re: Can this be combined into one statement? <derykus@gmail.com>
use MCE; <gamo@telecable.es>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 31 Oct 2013 13:58:03 -0500
From: John Black <jblack@nospam.com>
Subject: Re: Can this be combined into one statement?
Message-Id: <MPG.2cdc641dc3ed1bd989797@news.eternal-september.org>
In article <sov7ka-e3r2.ln1@anubis.morrow.me.uk>, ben@morrow.me.uk says...
>
> Quoth John Black <jblack@nospam.com>:
> >
> > Why does /(\S+)\s*$/ have to backtrack over "the whole string" whereas
> > /.*\s(\S+)/ does not?
> > I'm sure I don't undertand regex backtracking...
>
> Consider a string like "foo bar baz ". For /\S+\s*$/ perl tries the
> following sequence of matches:
>
> \S+ \s* $
> "foo" " " no match, backtrack
> "fo" "" no match, backtrack
> "f" "" no match, backtrack
>
> Now perl has tried all the matches starting at the beginning of the
> string, so it has to move along the string and try again. It skips over
> characters matching \S, since it's already tried all possible end-points
> for \S+ in this word, then it skips over characters not matching \S,
> since they can't possibly match, and starts again with:
>
> "bar" " " no match, backtrack
> "ba" "" no match, backtrack
> "b" "" no match, backtrack
>
> And again:
>
> "baz" " " match
>
> With more words in the string, or longer words, this would take more
> attempts. OTOH, with /.*\s\S+/ it tries these matches:
>
> .* \s \S+
> "foo bar baz " no match, backtrack
> "foo bar baz" " " no match, backtrack
> "foo bar ba" no match, backtrack
> "foo bar b" no match, backtrack
> "foo bar " no match, backtrack
> "foo bar" " " "baz"
>
> which only ever has to backtrack over the last word. In the specific
> case of a very long last word preceded by a small number of short words
> it would come out slower than the first match, but otherwise it comes
> out faster.
>
> You can see what perl is doing by running something like
>
> perl -Mre=debug -e'"foo bar baz " =~ /.*\s\S+/'
>
> though it takes a bit of practice to get used to interpreting the
> output.
>
> Ben
Ben, thanks for the detailed explanation. This is good stuff to keep in mind when in a
performance critical loop, but if I were doing this again, I would still go with /(\S+)\s*$/
because it is (to me) much more clear about what its doing. The $ anchor makes it obvious
that we are grabbing the word at the end of the line. The other regex matches every word on
the line and then you have to deduce that of those, the word it will return is the last one
becasue of Perl's default greedy matching policy. Which makes it less intuitive and
readable.
John Black
------------------------------
Date: Thu, 31 Oct 2013 19:16:38 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Can this be combined into one statement?
Message-Id: <mtmaka-ric.ln1@anubis.morrow.me.uk>
Quoth John Black <jblack@nospam.com>:
>
> Ben, thanks for the detailed explanation. This is good stuff to keep
> in mind when in a performance critical loop, but if I were doing this
> again, I would still go with /(\S+)\s*$/ because it is (to me) much
> more clear about what its doing. The $ anchor makes it obvious that
> we are grabbing the word at the end of the line. The other regex
> matches every word on the line and then you have to deduce that of
> those, the word it will return is the last one becasue of Perl's
> default greedy matching policy. Which makes it less intuitive and
> readable.
Exactly the right attitude :).
Ben
------------------------------
Date: Thu, 31 Oct 2013 16:20:50 -0700
From: Charles DeRykus <derykus@gmail.com>
Subject: Re: Can this be combined into one statement?
Message-Id: <l4uokv$gar$1@speranza.aioe.org>
On 10/30/2013 11:29 AM, Ben Morrow wrote:
>
> Quoth John Black <jblack@nospam.com>:
>>
>> Why does /(\S+)\s*$/ have to backtrack over "the whole string" whereas
>> /.*\s(\S+)/ does not?
>> I'm sure I don't undertand regex backtracking...
>
> Consider a string like "foo bar baz ". For /\S+\s*$/ perl tries the
> following sequence of matches:
>
> \S+ \s* $
> "foo" " " no match, backtrack
> "fo" "" no match, backtrack
> "f" "" no match, backtrack
>
> Now perl has tried all the matches starting at the beginning of the
> string, so it has to move along the string and try again. It skips over
> characters matching \S, since it's already tried all possible end-points
> for \S+ in this word, then it skips over characters not matching \S,
> since they can't possibly match, and starts again with:
>
> "bar" " " no match, backtrack
> "ba" "" no match, backtrack
> "b" "" no match, backtrack
>
> And again:
>
> "baz" " " match
>
> With more words in the string, or longer words, this would take more
> attempts.
> ...
I thought a possessive quantifier would help with this more intuitive
alternative: (\S+)\s*$ -> (\S++)\s*+$. But, unless there's some basic
error on my part, then the possessive replacement ate the proverbial
dust even of the backtracking regex. Maybe there are still caching
issues as mentioned here:
http://www.perlmonks.org/bare/?node_id=664545
--
Charles DeRykus
------------------------------
Date: Thu, 31 Oct 2013 16:27:10 +0100
From: gamo <gamo@telecable.es>
Subject: use MCE;
Message-Id: <l4tssm$bmn$1@speranza.aioe.org>
Hello
I usually do a lot of simulations and heuristics in a linear funcional
way. Now I want to send random candidates to the cores or process, get
the results for each one and when the acceptable result is reached,
rerun the calculus, print a report and exit.
I take a look on the modules Parallel::* (Simple, ForkManager and Loops)
but none is both easy and could do the things well. Then I see MCE, wich
sure could do that things, but is complex and the MCE::Examples provided
don't cover a simple simulation scenario.
Are there somebody wich could guide me in the parallelization of
something like
$var = 1;
$foo = 2;
$bar = 3;
while (1) {
$r = rand();
$result = $r * ($var+$foo+$bar);
$anothervar = sqrt($var+$foo+$bar);
last if ($result < $anothervar+0.001 && $result > $anothervar-0.001);
}
print "$r -> $result\n";
exit ;
Thanks in advance
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 4068
***************************************