#!/usr/bin/env perl

use Modern::Perl;
use Benchmark qw(:all);

use Data::Util qw(is_instance);
use Params::Util qw(_INSTANCE);
use Ref::Util::XS qw(is_blessed_ref);
use Safe::Isa;
use Scalar::Util qw(blessed);
use Symbol;

my @vals = ( undef, '', [], {}, gensym, qr//, (bless {}, 'AAA') );

my $count = -1;

say "--> isa 判定";
cmpthese($count, {
    blessed => sub {
        blessed($_) and $_->isa('AAA') for @vals;
    },
    is_instance => sub {
        is_instance($_, 'AAA') for @vals;
    },
    _INSTANCE => sub {
        _INSTANCE($_, 'AAA') for @vals;
    },
    '$_isa' => sub {
        $_->$_isa('AAA') for @vals;
    },
    is_blessed_ref => sub {
        is_blessed_ref($_) and $_->isa('AAA') for @vals;
    },
});

say "";
say "--> bless されているか判定";
cmpthese($count, {
    blessed => sub {
        blessed($_) for @vals;
    },
    is_blessed_ref => sub {
        is_blessed_ref($_) for @vals;
    },
});

__END__

% perl benchmark/blessed.pl
--> isa 判定
                    Rate     $_isa _INSTANCE  blessed is_instance is_blessed_ref
$_isa           328504/s        --      -51%     -68%        -82%           -83%
_INSTANCE       672289/s      105%        --     -34%        -62%           -65%
blessed        1013874/s      209%       51%       --        -43%           -48%
is_instance    1789569/s      445%      166%      77%          --            -8%
is_blessed_ref 1936331/s      489%      188%      91%          8%             --

--> bless されているか判定
                    Rate        blessed is_blessed_ref
blessed        1626881/s             --           -71%
is_blessed_ref 5694085/s           250%             --

% perl -v | head -2

This is perl 5, version 28, subversion 2 (v5.28.2) built for amd64-freebsd-thread-multi

コメントする

perl adv
perl adv