in reply to Validating Numbers in CGI Script?
Whereas both of the packs output:4.34 -2.33333 5 20
4.34 -2.33333 0 5 0 5 12 0 0 0 20 4.3e+015
use Benchmark; sub regexstuff() { map {$foo = $_ if /^-?(?=\d|\.\d)\d*\.?\d+$/} (qw/4.34 -2.33333 abc 5 maurice 5_5_4_1 12peter peter12 shoe fred 20 4.3E15/); } sub packstuffD() { map { $foo = unpack ("d", pack ("d", $_)), "\n" } (qw/4.34 -2.33333 abc 5 maurice 5_5_4_1 12peter peter12 shoe fred 20 4.3E15/); } sub packstuffF() { map { $foo = unpack ("f", pack ("f", $_)), "\n" } (qw/4.34 -2.33333 abc 5 maurice 5_5_4_1 12peter peter12 shoe fred 20 4.3E15/); } sub evalstuff() { map { eval{ local $^W = 1; $_ + 0 } } (qw/4.34 -2.33333 abc 5 maurice 5_5_4_1 12peter peter12 shoe fred 20 4.3E15/); if ( $@ ) { $result = "$invalue is *not* a Number; Details: $@" } else { $result = "$invalue appears to be a number."; } } timethese ( 100000, { packstuffD => "packstuffD", regex => "regexstuff", eval => "evalstuff", packstuffF => "packstuffF" } ); eval: 13 wallclock secs (11.97 usr + 0.02 sys = 11.99 CPU) @ 83 +42.37/s (n=100000) packstuffD: 11 wallclock secs (10.06 usr + 0.00 sys = 10.06 CPU) @ 99 +45.30/s (n=100000) packstuffF: 10 wallclock secs (10.63 usr + 0.02 sys = 10.65 CPU) @ 93 +93.20/s (n=100000) regex: 11 wallclock secs (11.01 usr + 0.01 sys = 11.02 CPU) @ 90 +77.71/s (n=100000)
|
|---|