Often there are other considerations. For instance, if you were doing the s/// on dashes preceeded and followed by digits, what you expect from "1-2-3" would dictate which to use.
The speed difference is not due so much to capturing or not but to the fact that substituting a constant string (with /g) loops inside the subst opcode, while interpolating a variable builds a loop of other opcodes, just as if you had s///e:
$ perl -MO=Concise,-exec -we's/(?<=\d)-(?=[A-Z])//ig'
1 <0> enter
2 <;> nextstate(main 1 -e:1) v
3 <$> const[PV ""] s
4 </> subst(/"(?<=\\d)-(?=[A-Z])"/) vK
5 <@> leave[1 ref] vKP/REFC
-e syntax OK
$ perl -MO=Concise,-exec -we's/(\d)-([A-Z])/$1$2/ig'
1 <0> enter
2 <;> nextstate(main 1 -e:1) v
3 </> subst(/"(\\d)-([A-Z])"/ replstart->4) v
4 <#> gvsv[*1] s
5 <#> gvsv[*2] s
6 <2> concat[t3] sK/2
7 <|> substcont(other->3) sK/1
8 <@> leave[1 ref] vKP/REFC
-e syntax OK
But even so, remember that optimization should be one of the last steps in development. Write the code however is most clear to you; then, only if there is a performance problem, do some profiling and optimization.
(But if either way is equally clear and equally easy, obviously go with whichever you think may be faster.)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|