substr vs regex
paulm at paulm.com
Tue Sep 4 19:49:07 BST 2007
On 9/3/07, Ovid <publiustemp-londonpm at yahoo.com> wrote:
> --- alex at owal.co.uk wrote:
> > Imagine, say, someone wanted the last three characters of a string.
> > They
> > might use a regex /(...)$/ or substr($variable, -3)
> > personally I recommend the former because it is more readable, but
> > others
> > have recommended substr to because you "dont need to bring out the
> > big
> > guns". (However no one could actually remember the substr function)
> > Is one of these significantly more efficient than the other? Ok ok,
> > so I
> > could probably write a test in a few minutes but asking you guys is
> > more
> > interesting.
> Define "efficient". The definition varies tremendously depending on
> your needs. Optimization order:
> 1. Correctness.
> 2. Readability.
> 217. Performance :)
> It's far more important that your code be correct and legible than
Surely it depends on the application.
Also, if your code is running on thousands of machines, the cost of
that lack of optimisation can rapidly exceed the salaries of the
people who wrote it, not to mention burning the team's carbon karma...
Admittedly this isn't a situation most coders run into but it's
interesting at least as a thought experiment about where the
boundaries of our dogma should be challenged.
> When you have the first two in place, don't worry about speed if
> it's fast enough. If it's not, then you can benchmark and find out
> where it's *really* slow (and I doubt it will be a 'regex' versus
> 'substr' issue).
XML v SQL :-)
> Buy the book - http://www.oreilly.com/catalog/perlhks/
> Perl and CGI - http://users.easystreet.com/ovid/cgi_course/
> Personal blog - http://publius-ovidius.livejournal.com/
> Tech blog - http://use.perl.org/~Ovid/journal/
More information about the london.pm