Besides, the "eq" is actually converting the integer back to a string, which will cost performance.
#!/usr/bin/env perl
use strict;
use warnings;
use Benchmark 'timethis';
timethis (10_000_000, "valid_int ('18446744073709551614')");
sub valid_int {
my $num = shift;
return unless $num =~ /^\d+$/a;
return int $num eq $num;
}
__END__
timethis 10000000: 8 wallclock secs ( 7.93 usr + 0.00 sys = 7.93 CP
+U) @ 1261034.05/s (n=10000000)
So, less than a microsecond on my aging system. Doubtless this can be improved upon (although about half the time taken appears to be the regex so there's a limit there too).
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|