Work in progress to make Moose accept Type::Constraint objects for the isa param
[gitmo/Moose.git] / benchmarks / caf_vs_moose.pl
1 #!perl
2
3 ### MODULES
4
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');
14     __PACKAGE__->meta->make_immutable();
15 }
16 {
17     package MooseImmutable::NoConstructor;
18     use Moose;
19     has foo => (is => 'rw');
20     __PACKAGE__->meta->make_immutable(inline_constructor => 0);
21 }
22 {
23     package ClassAccessorFast;
24     use warnings;
25     use strict;
26     use base 'Class::Accessor::Fast';
27     __PACKAGE__->mk_accessors(qw(foo));
28 }
29
30 use Benchmark qw(cmpthese);
31 use Benchmark ':hireswallclock';
32
33 my $moose                = PlainMoose->new;
34 my $moose_immut          = MooseImmutable->new;
35 my $moose_immut_no_const = MooseImmutable::NoConstructor->new;
36 my $caf                  = ClassAccessorFast->new;
37
38 my $acc_rounds = 100_000;
39 my $ins_rounds = 100_000;
40
41 print "\nSETTING\n";
42 cmpthese($acc_rounds, {
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) },
47 }, 'noc');
48
49 print "\nGETTING\n";
50 cmpthese($acc_rounds, {
51     Moose                       => sub { $moose->foo },
52     MooseImmutable              => sub { $moose_immut->foo },
53     MooseImmutableNoConstructor => sub { $moose_immut_no_const->foo },
54     ClassAccessorFast           => sub { $caf->foo },
55 }, 'noc');
56
57 my (@moose, @moose_immut, @moose_immut_no_const, @caf_stall);
58 print "\nCREATION\n";
59 cmpthese($ins_rounds, {
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}) },
64 }, 'noc');
65
66 my ( $moose_idx, $moose_immut_idx, $moose_immut_no_const_idx, $caf_idx ) = ( 0, 0, 0, 0 );
67 print "\nDESTRUCTION\n";
68 cmpthese($ins_rounds, {
69     Moose => sub {
70         $moose[$moose_idx] = undef;
71         $moose_idx++;
72     },
73     MooseImmutable => sub {
74         $moose_immut[$moose_immut_idx] = undef;
75         $moose_immut_idx++;
76     },
77     MooseImmutableNoConstructor => sub {
78         $moose_immut_no_const[$moose_immut_no_const_idx] = undef;
79         $moose_immut_no_const_idx++;
80     },
81     ClassAccessorFast   => sub {
82         $caf_stall[$caf_idx] = undef;
83         $caf_idx++;
84     },
85 }, 'noc');
86
87