Blog

benchmarking Variable::Magic and tie

I taken benchmark between Variable::Magic and tie.

In this case, Variable::Magic is 3.1x faster than tie. awesome.

perl: 5.012000
Tie::Hash: 1.03
Variable::Magic: 0.41

           Rate   tie magic
tie    607350/s    --  -76%
magic 2522142/s  315%    --
use 5.12.0;
use Benchmark qw/:all/;
use Variable::Magic qw/wizard cast/;
use Tie::Hash;

say "perl: $]";
say "Tie::Hash: $Tie::Hash::VERSION";
say "Variable::Magic: $Variable::Magic::VERSION";
say "";

{
    package THash;
    use parent -norequire => 'Tie::StdHash';
    sub STORE {
        my ($self, $key, $val) = @_;
        $self->{$key} = $val;
        # warn $key;
    }
}

tie my %hash, 'THash';
my $wiz = wizard 'store' => sub {
# warn "STORE";
        };
my %v;
cast(%v, $wiz);
cmpthese(
    -1 => {
        'tie' => sub {
            $hash{foo} = 1;
        },
        'magic' => sub {
            $v{foo} = 1;
        },
    },
);