File: instance_bench.pl

package info (click to toggle)
libdata-util-perl 0.67-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 556 kB
  • sloc: perl: 2,958; ansic: 416; makefile: 8
file content (69 lines) | stat: -rw-r--r-- 1,093 bytes parent folder | download | duplicates (5)
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
#!perl -w

use strict;
use Benchmark qw(:all);

use FindBin qw($Bin);
use lib $Bin;
use Common;

use Data::Util qw(:all);
use Params::Util qw(_INSTANCE); # 0.35 provides a XS implementation
use Scalar::Util qw(blessed);

signeture
	'Data::Util'   => \&is_instance,
	'Params::Util' => \&_INSTANCE,
	'Scalar::Util' => \&blessed,
;

BEGIN{
	package Base;
	sub new{
		bless {} => shift;
	}
	
	package Foo;
	our @ISA = qw(Base);
	package Foo::X;
	our @ISA = qw(Foo);
	package Foo::X::X;
	our @ISA = qw(Foo::X);
	package Foo::X::X::X;
	our @ISA = qw(Foo::X::X);

	package Unrelated;
	our @ISA = qw(Base);

	package SpecificIsa;
	our @ISA = qw(Base);
	sub isa{
		$_[1] eq 'Foo';
	}
}

foreach my $x (Foo->new, Foo::X::X::X->new, Unrelated->new, undef, {}){
	print 'For ', neat($x), "\n";

	my $i = 0;

	cmpthese -1 => {
		'blessed' => sub{
			for(1 .. 10){
				$i++ if blessed($x) && $x->isa('Foo');
			}
		},
		'_INSTANCE' => sub{
			for(1 .. 10){
				$i++ if _INSTANCE($x, 'Foo');
			}
		},
		'is_instance' => sub{
			for(1 .. 10){
				$i++ if is_instance($x, 'Foo');
			}
		},
	};

	print "\n";
}