Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use 5.16.1;
- use B;
- use constant DEF_INTEGER_AS_STRING => 4352;
- use constant DEF_POSITIVE_INTEGER => 1;
- use Scalar::Util::LooksLikeNumber qw(looks_like_number);
- use Benchmark qw(cmpthese);
- sub is_pos_integer_ant_gist {
- # return unless @_ and defined $_[0];
- my $lln = looks_like_number($_[0]);
- ($lln == 1 || $lln & B::SVf_IOK) && $_[0] > 0;
- }
- sub is_pos_integer_ant_tg {
- # return unless @_ and defined $_[0];
- return ( $_[0] eq int( $_[0] ) && $_[0] > 0 );
- }
- sub is_pos_integer_akk {
- return unless @_ and defined $_[0];
- my $fl;
- $fl = looks_like_number($_[0])
- and (( $fl == DEF_INTEGER_AS_STRING and index($_[0], '-') < 0 ) or $fl == DEF_POSITIVE_INTEGER)
- }
- my @ptrn = (undef, 123, -123, "789", "-789", "7.87", "-7.87", {"a"=>"b"}, sub {}, "13234-56qwer");
- my @test_seq = map $ptrn[int(rand @ptrn)], 1..1000;
- sub test_func {
- my ($test_sub, $test_sq) = @_;
- my @res = map $test_sub->($_), @{$test_sq}
- }
- cmpthese(1e+6, {
- 'ant_gist' => q<test_func(\&is_pos_integer_ant_gist, \@test_seq)>,
- 'ant_tg' => q<test_func(\&is_pos_integer_ant_tg, \@test_seq)>,
- 'akk' => q<test_func(\&is_pos_integer_akk, \@test_seq)>,
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement