Advertisement
DRVTiny

bench_is_posint.pl

Apr 25th, 2018
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.21 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use 5.16.1;
  3. use B;
  4. use constant DEF_INTEGER_AS_STRING => 4352;
  5. use constant DEF_POSITIVE_INTEGER  => 1;
  6. use Scalar::Util::LooksLikeNumber qw(looks_like_number);
  7. use Benchmark qw(cmpthese);
  8.  
  9.  
  10. sub is_pos_integer_ant_gist {
  11.  #   return unless @_ and defined $_[0];
  12.     my $lln = looks_like_number($_[0]);
  13.     ($lln == 1 || $lln & B::SVf_IOK) && $_[0] > 0;
  14. }
  15.  
  16. sub is_pos_integer_ant_tg {
  17. #    return unless @_ and defined $_[0];
  18.     return ( $_[0] eq int( $_[0] ) && $_[0] > 0 );
  19. }
  20.  
  21. sub is_pos_integer_akk {
  22.     return unless @_ and defined $_[0];
  23.     my $fl;
  24.     $fl = looks_like_number($_[0])
  25.         and (( $fl == DEF_INTEGER_AS_STRING and index($_[0], '-') < 0 ) or $fl == DEF_POSITIVE_INTEGER)
  26. }
  27.  
  28. my @ptrn = (undef, 123, -123, "789", "-789", "7.87", "-7.87", {"a"=>"b"}, sub {}, "13234-56qwer");
  29. my @test_seq = map $ptrn[int(rand @ptrn)], 1..1000;
  30.  
  31. sub test_func {
  32.     my ($test_sub, $test_sq) = @_;
  33.     my @res = map $test_sub->($_), @{$test_sq}  
  34. }
  35.  
  36. cmpthese(1e+6, {
  37.     'ant_gist'  => q<test_func(\&is_pos_integer_ant_gist, \@test_seq)>,
  38.     'ant_tg'    => q<test_func(\&is_pos_integer_ant_tg, \@test_seq)>,
  39.     'akk'   => q<test_func(\&is_pos_integer_akk, \@test_seq)>,    
  40. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement