Leser: 33
die('Invalid integer') unless $page_id =~ m/^\d+$/;
2010-02-22T22:21:08 pktmCode: (dl )die('Invalid integer') if $page_id =~ m/^\d+$/;
2010-02-22T23:41:08 pktmallerdings war ich auch mehr an anderen Ansätzen als dem der RegEx interessiert.
2010-02-22T23:09:03 Froschpopoif ($zahl == int($zahl))
1
2
3
unless (length($page_id) && $page_id =~ y/0-9// == length($page_id)) {
die "nix zahl\n";
}
length($page_id) and not $page_id =~ y/0-9//c;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); my %tests=( '6407587026'=>1, '97698bhj767565'=>0, '7867854jh6546789'=>0, '86565'=>1, '54587'=>1, 'ghggftr'=>0, '557765547'=>1, 'g569876876'=>0, '656778z6565'=>0, '6578867ggh7'=>0, '54tf5945'=>0, '67556567'=>1, '765476'=>1, '*Ä?=()&§9§""¹³¹²³½[}\¼'=>0, '454564'=>1, '547989'=>1, '709'=>1, '=)(/(&$§"%%/)(=/(/'=>0, ); my @zahlen=keys(%tests); my %functs=( 'split1'=>\&test_of_number_a, 'split2'=>\&test_of_number_b, 'regexp'=>\&test_of_number_c, ' int'=>\&test_of_number_d, 'unpack'=>\&test_of_number_e, ' tr'=>\&test_of_number_f, ); while( my ($name,$func)=each(%functs)) { my $ok=1; while(my ($num,$res)=each(%tests)) { $ok=0 if($func->($num)!=$res); } print "$name => ",$ok?'OK':'FAILED',"\n"; $functs{$name}=sub{ $func->($_) for(@zahlen); }; } cmpthese(40000,\%functs); ########################################################################################## sub test_of_number_a { for my $pos (0..length($_[0])-1) { $_=ord(substr($_[0],$pos,1)); return 0 if($_<48 || 57<$_); } return 1; } sub test_of_number_b { for(split('',''.shift())) { $_=ord($_); return 0 if($_<48 || 57<$_); } return 1; } sub test_of_number_c { return 1 if($_[0]=~/^\d+$/); return 0; } sub test_of_number_d { no warnings; return 1 if(int($_[0]) eq $_[0]); return 0; } sub test_of_number_e { ($_<48 || 57<$_)?return 0:undef() for(unpack('C*',$_[0])); return 1; } sub test_of_number_f { return 1 if($_[0]=~ y/0-9// == length($_[0])); return 0; }
1
2
3
4
5
6
7
Rate split2 split1 unpack regexp int tr
split2 12308/s -- -8% -32% -66% -68% -72%
split1 13423/s 9% -- -26% -63% -65% -69%
unpack 18100/s 47% 35% -- -50% -52% -59%
regexp 36036/s 193% 168% 99% -- -5% -18%
int 38095/s 210% 184% 110% 6% -- -13%
tr 43956/s 257% 227% 143% 22% 15% --
2010-02-23T09:45:51 topegUnd du kannst sicher auch erraten warum?
1 2 3 4 5
sub test_of_number_e { ($_<48 || 57<$_)?return 0:undef() for(unpack('C*',$_[0])); return 1; }
2010-02-23T10:04:25 Linuxerhab den Code nur fix überflogen, dabei fiel mir auf, dass Du in einer Sub mal $_ und mal $_[0] verwendest. Ist das Flüchtigkeit oder Absicht?
1 2 3 4 5
sub test_of_number_e { ($_<48 || 57<$_)?return 0:undef() for(unpack('C*',$_[0])); return 1; }
1 2 3 4 5
sub test_of_number_e { for my $char (unpack('C*',$_[0])){ ($char<48 || 57<$char)?return 0:undef(); } return 1; }
1 2 3 4 5
sub test_of_number_e { for my $char (unpack('C*',$_[0])){ return 0 if($char<48 || 57<$char); } return 1; }
1 2 3 4 5 6 7 8 9 10 11
sub test_of_number_e { my $number=$_[0]; for my $char (unpack('C*',$number)) { # 'ASCII 0' = 48 # 'ASCII 9' = 57 return 0 if($char<48 || 57<$char); } return 1; }
1 2 3 4 5 6 7 8 9 10
use Inline C => <<'END_C'; int is_int(char* str) { int i=0; char c; while(c = str[i++]) { if (c < '0' || c > '9') return 0; } return 1; } END_C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Rechner1
Rate split2 split1 unpack tr regexp int
split2 31513/s -- -25% -39% -75% -78% -79%
split1 41945/s 33% -- -19% -67% -70% -72%
unpack 51602/s 64% 23% -- -59% -64% -65%
tr 126879/s 303% 202% 146% -- -11% -14%
regexp 142104/s 351% 239% 175% 12% -- -4%
int 148055/s 370% 253% 187% 17% 4% --
# Rechner2
Rate split2 unpack split1 int tr regexp
split2 12238/s -- -15% -18% -74% -74% -76%
unpack 14471/s 18% -- -3% -69% -70% -72%
split1 14941/s 22% 3% -- -68% -69% -71%
int 46690/s 282% 223% 212% -- -2% -9%
tr 47752/s 290% 230% 220% 2% -- -7%
regexp 51210/s 318% 254% 243% 10% 7% --