[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Drunk Driving redux (what was he thinking?)



Vicki Brown wrote:
[snip]

! When I use index it's because I want to know the position of one
! string inside another.  When I see index() I conclude that the
! _value_ of the index, i.e. the position of the substring, is
! important. I use index if I want to determine _Where_.
!
! I don't use index() when I simply want to know the truth value (is
! string a contained in string b). When I want to know the latter (and
! I don't care where, only if), I use a pattern match. I use pattern
! matching if I want to determine _If_.

I tend to think index() when looking for strings and m// when
looking for patterns --- I recall discussions a couple years ago
about efficieny differences between m// and index() where m// was
unecessarily slower when the pattern was a literal string (it was
either fixed or never really a problem, I don't recall), so perhaps
the code in question was an attempt (probably misbegotten) to
optimize for a most likely case of failure and only drag in the regex
engine when necessary.

FWIW, I reran Ronald's simple benchmark (under 5.00503) using
'timethese(-10,...)' to run for at least 10 seconds per snippet and
found (as opposed to his run) that index() was just marginally faster
in both cases:

 index_l: [snip] @ 36007.09/s (n=360431)
 index_s: [snip] @ 52871.95/s (n=537179)
 regex_l: [snip] @ 33362.50/s (n=333625)
 regex_s: [snip] @ 48539.26/s (n=485878)

Not enough to worry about in any case.

regards
andrew

-- 
Andrew L. Johnson   http://members.home.com/andrew-johnson/
      The generation of random numbers is too 
      important to be left to chance.

==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org