Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: lookahead / lookbehind vs other regex methods

by ysth (Canon)
on Jan 08, 2004 at 00:17 UTC ( [id://319667]=note: print w/replies, xml ) Need Help??


in reply to lookahead / lookbehind vs other regex methods

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.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-18 10:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found