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
|
#!/usr/bin/perl -w
BEGIN {
$ENV{MOO_XS_DISABLE} = "no cheating";
$ENV{MOUSE_PUREPERL} = "no cheating";
}
package Bench::Base;
sub new {
my($class) = shift;
bless { test => 23 }, $class;
}
package Bench::Direct;
use base qw(Bench::Base);
package Bench::Normal;
use Class::Accessor "moose-like";
has test => (is => "rw");
package Bench::Fast;
use Class::Accessor::Fast "moose-like";
has test => (is => "rw");
package Bench::Faster;
use Class::Accessor::Faster "antlers";
has test => (is => "rw");
package Bench::Moose;
use Moose;
has test => (is => "rw");
package Bench::Mouse;
use Mouse;
has test => (is => "rw");
package Bench::Moo;
use Moo;
has test => (is => "rw");
package main;
use strict;
use Benchmark 'cmpthese';
use Test::More tests => 12;
my $tmp;
my $direct = Bench::Direct->new({ test => 23 });
my %accessor = ( Direct => sub { $tmp = $direct->{test}; } );
my %mutator = ( Direct => sub { $direct->{test} = 42; } );
for my $p (qw/Normal Fast Faster Moose Mouse Moo/) {
my $o = "Bench::$p"->new({ test => 23 });
is $o->test, 23, "$p init";
$o->test(24);
is $o->test, 24, "$p set";
$accessor{$p} = sub { $tmp = $o->test; };
$mutator{$p} = sub { $o->test(42); };
}
print "accessors:\n";
cmpthese( -1, \%accessor );
print "\n";
print "mutators:\n";
cmpthese( -1, \%mutator );
|