]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - samples/vm_vs_tie.pl
Importing Variable-Magic-0.10.tar.gz
[perl/modules/Variable-Magic.git] / samples / vm_vs_tie.pl
1 #!/usr/bin/env perl
2
3 use strict;
4 use warnings;
5
6 use Tie::Hash;
7
8 use lib qw{blib/arch blib/lib};
9 use Variable::Magic qw/wizard cast VMG_UVAR/;
10
11 use Benchmark qw/cmpthese/;
12
13 die 'Your perl does not support the nice uvar magic of 5.10.*' unless VMG_UVAR;
14
15 my @a = ('a' .. 'z');
16
17 tie my %t, 'Tie::StdHash';
18 $t{$a[$_]} = $_ for 0 .. $#a;
19
20 my $wiz = wizard fetch => sub { 0 }, store => sub { 0 };
21 my %v;
22 $v{$a[$_]} = $_ for 0 .. $#a;
23 cast %v, $wiz;
24
25 cmpthese -3, {
26  'tie'  => sub { my ($x, $y) = map @a[rand @a], 1 .. 2; my $a = $t{$x}; $t{$y} = $a },
27  'v::m' => sub { my ($x, $y) = map @a[rand @a], 1 .. 2; my $a = $v{$x}; $v{$y} = $a }
28 };