On Mon, Nov 06, 2006 at 01:28:16PM +0100, Alexander Foken wrote:
> [EMAIL PROTECTED] wrote:
> >Lo all,
> >
> >Which is faster for checking that user input is numeric, using the 
> >look_like_number function or a compiled regex?
> >The number in question is a positive 4 digit integer.
>
> I think you can beat /^\d{4}$/ only by using a carefully crafted piece 
> of C code.

And, strangely enough, look_like_number is a carefully crafted piece of
C code. In fact it uses the carefully crafted piece of C code that perl
itself uses.

> >Which would be faster if I also had to check that the number was
> >in 24hr clock format?
> >ie one regex to do that or looks_like_number followed by another
> >regex?
> 
> Like above: /^\d{2}:\d[2}$/ without seconds, /^\d{2}:\d[2}:\d{2}$/ with 
> seconds. Note that those REs do not check the values, 99:99 would be 
> accepted as a valid 24h clock value. To check the values, do something 
> like this:
> 
> $value=~/^(\d{2}):(\d[2}):(\d{2})$/ or die "not 24h format";
> $1<24 or die "invalid hour value";
> $2<60 or die "invalid minute value";
> $3<60 or die "invalid second value"; # 62 if you are pedantic and want 
> to accept leap seconds

All true (ignoring the typos - [ instead of {) but it's also worth
pointing out two key issues with perl performance questions:

 - Performance is very hard to predict - so measure it yourself.
   The Benchmark module is one way to do that.

 - Premature optimization is a bad idea ("root of all evil" blah blah).
   Write your code to be easy to maintain first and foremost.
   Rewrite code for performance if, and only if, there's a measurable
   performance problem with that code - generally there won't be.
   Profiling tools like Devel::DProf and Devel::SmallProf can help.

Tim.

Reply via email to