Add version and release date
[gitmo/Moose.git] / benchmarks / caf_vs_moose.pl
CommitLineData
b584a6bc 1#!perl
2
3### MODULES
4
734d1752 5{
6 package PlainMoose;
7 use Moose;
8 has foo => (is => 'rw');
9}
10{
11 package MooseImmutable;
12 use Moose;
13 has foo => (is => 'rw');
5a3217de 14 __PACKAGE__->meta->make_immutable();
734d1752 15}
16{
17 package MooseImmutable::NoConstructor;
18 use Moose;
19 has foo => (is => 'rw');
5a3217de 20 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
734d1752 21}
22{
23 package ClassAccessorFast;
24 use warnings;
25 use strict;
26 use base 'Class::Accessor::Fast';
27 __PACKAGE__->mk_accessors(qw(foo));
28}
b584a6bc 29
30use Benchmark qw(cmpthese);
31use Benchmark ':hireswallclock';
32
734d1752 33my $moose = PlainMoose->new;
34my $moose_immut = MooseImmutable->new;
35my $moose_immut_no_const = MooseImmutable::NoConstructor->new;
36my $caf = ClassAccessorFast->new;
b584a6bc 37
734d1752 38my $acc_rounds = 100_000;
39my $ins_rounds = 100_000;
b584a6bc 40
41print "\nSETTING\n";
42cmpthese($acc_rounds, {
734d1752 43 Moose => sub { $moose->foo(23) },
44 MooseImmutable => sub { $moose_immut->foo(23) },
45 MooseImmutableNoConstructor => sub { $moose_immut_no_const->foo(23) },
46 ClassAccessorFast => sub { $caf->foo(23) },
b584a6bc 47}, 'noc');
48
49print "\nGETTING\n";
50cmpthese($acc_rounds, {
734d1752 51 Moose => sub { $moose->foo },
52 MooseImmutable => sub { $moose_immut->foo },
53 MooseImmutableNoConstructor => sub { $moose_immut_no_const->foo },
54 ClassAccessorFast => sub { $caf->foo },
b584a6bc 55}, 'noc');
56
57my (@moose, @moose_immut, @moose_immut_no_const, @caf_stall);
58print "\nCREATION\n";
59cmpthese($ins_rounds, {
734d1752 60 Moose => sub { push @moose, PlainMoose->new(foo => 23) },
61 MooseImmutable => sub { push @moose_immut, MooseImmutable->new(foo => 23) },
62 MooseImmutableNoConstructor => sub { push @moose_immut_no_const, MooseImmutable::NoConstructor->new(foo => 23) },
63 ClassAccessorFast => sub { push @caf_stall, ClassAccessorFast->new({foo => 23}) },
b584a6bc 64}, 'noc');
65
66my ( $moose_idx, $moose_immut_idx, $moose_immut_no_const_idx, $caf_idx ) = ( 0, 0, 0, 0 );
67print "\nDESTRUCTION\n";
68cmpthese($ins_rounds, {
69 Moose => sub {
70 $moose[$moose_idx] = undef;
71 $moose_idx++;
72 },
734d1752 73 MooseImmutable => sub {
b584a6bc 74 $moose_immut[$moose_immut_idx] = undef;
75 $moose_immut_idx++;
76 },
734d1752 77 MooseImmutableNoConstructor => sub {
b584a6bc 78 $moose_immut_no_const[$moose_immut_no_const_idx] = undef;
79 $moose_immut_no_const_idx++;
80 },
734d1752 81 ClassAccessorFast => sub {
b584a6bc 82 $caf_stall[$caf_idx] = undef;
83 $caf_idx++;
84 },
85}, 'noc');
86
87