Subject: | UNIVERSAL::isa() for reference type discovery is slow |
Hello,
Just a little something I noticed that might help a bit
When trying to discover a reference's type, instead of this
UNIVERSAL::isa($x, 'HASH')
do this:
ref $x eq 'HASH'
It's over twice as fast checking vars that are references and almost 3 times when checking
vars that are not references:
Rate isa ref isa nr ref nr
isa 3521127/s -- -54% -60% -74%
ref 7692308/s 118% -- -12% -44%
isa nr 8771930/s 149% 14% -- -36%
ref nr 13698630/s 289% 78% 56% --
Subject: | isa_eq.pl |
#!/usr/bin/perl
use Benchmark;
my $hr = {};
my $nr;
Benchmark::cmpthese(10_000_000, {
'ref' => sub { if(ref $hr eq 'HASH') {} },
'isa' => sub { if(UNIVERSAL::isa($hr, 'HASH')) {} },
'ref nr' => sub { if(ref $nr eq 'HASH') {} },
'isa nr' => sub { if(UNIVERSAL::isa($nr, 'HASH')) {} },
});