looks_like_number function

looks_like_number function

am 06.11.2006 12:55:00 von John1

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.

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?

Thx

John

Re: looks_like_number function

am 06.11.2006 13:28:16 von Alexander

john1@firstb2b.net 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.
> 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


Alexander

--
Alexander Foken
mailto:alexander@foken.de http://www.foken.de/alexander/

Re: looks_like_number function

am 06.11.2006 14:16:15 von Tim.Bunce

On Mon, Nov 06, 2006 at 01:28:16PM +0100, Alexander Foken wrote:
> john1@firstb2b.net 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.

Re: looks_like_number function

am 06.11.2006 15:28:42 von Jenda

From: Alexander Foken
> john1@firstb2b.net 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. > 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


$value =~ /^(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d$/
or die "not 24h format";

Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery

Re: looks_like_number function

am 06.11.2006 15:43:18 von John1

On 6 Nov 2006 at 13:16, Also Sprach Tim Bunce:

> And, strangely enough, look_like_number is a carefully crafted piece of
> C code.

I thought it might be.

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

I'll look into it. Though it is fair to say I am splitting the ninth of a
gnats cock with this question.

> - Premature optimization is a bad idea ("root of all evil" blah blah).

I have to disagree with that one.

> Rewrite code for performance if, and only if, there's a measurable
> performance problem with that code - generally there won't be.

heh, you should see some of my code! ;)

Thanks.

John