Improve benchmark (don't include object construction in accessor measurement)
[gitmo/Moose.git] / benchmarks / type_constraints2.pl
CommitLineData
6fe30475 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Benchmark qw[timethese];
7
8=pod
9
10This benchmark is designed to measure how long things with type constraints
11take (constructors, accessors). It was created to measure the impact of
12inlining type constraints.
13
14=cut
15
16{
17 package Thing;
18
19 use Moose;
20
21 has int => (
22 is => 'rw',
23 isa => 'Int',
24 );
25
26 has str => (
27 is => 'rw',
28 isa => 'Str',
29 );
30
31 has fh => (
32 is => 'rw',
33 isa => 'FileHandle',
34 );
35
36 has object => (
37 is => 'rw',
38 isa => 'Object',
39 );
40
41 has a_int => (
42 is => 'rw',
43 isa => 'ArrayRef[Int]',
44 );
45
46 has a_str => (
47 is => 'rw',
48 isa => 'ArrayRef[Str]',
49 );
50
51 has a_fh => (
52 is => 'rw',
53 isa => 'ArrayRef[FileHandle]',
54 );
55
56 has a_object => (
57 is => 'rw',
58 isa => 'ArrayRef[Object]',
59 );
60
61 has h_int => (
62 is => 'rw',
63 isa => 'HashRef[Int]',
64 );
65
66 has h_str => (
67 is => 'rw',
68 isa => 'HashRef[Str]',
69 );
70
71 has h_fh => (
72 is => 'rw',
73 isa => 'HashRef[FileHandle]',
74 );
75
76 has h_object => (
77 is => 'rw',
78 isa => 'HashRef[Object]',
79 );
80
81 __PACKAGE__->meta->make_immutable;
82}
83
84my @ints = 1 .. 10;
85my @strs = 'a' .. 'j';
86my @fhs = map { my $fh; open $fh, '<', $0 or die; $fh; } 1 .. 10;
87my @objects = map { Thing->new } 1 .. 10;
88
89my %ints = map { $_ => $_ } @ints;
90my %strs = map { $_ => $_ } @ints;
91my %fhs = map { $_ => $_ } @fhs;
92my %objects = map { $_ => $_ } @objects;
93
a965b6da 94my $thing = Thing->new;
95
6fe30475 96timethese(
97 200_00, {
98 constructor => sub {
99 Thing->new(
100 int => $ints[0],
101 str => $strs[0],
102 fh => $fhs[0],
103 object => $objects[0],
104 a_int => \@ints,
105 a_str => \@strs,
106 a_fh => \@fhs,
107 a_object => \@objects,
108 h_int => \%ints,
109 h_str => \%strs,
110 h_fh => \%fhs,
111 h_object => \%objects,
112 );
113 },
114 accessors => sub {
6fe30475 115 $thing->int( $ints[0] );
116 $thing->str( $strs[0] );
117 $thing->fh( $fhs[0] );
118 $thing->object( $objects[0] );
119 $thing->a_int( \@ints );
120 $thing->a_str( \@strs );
121 $thing->a_fh( \@fhs );
122 $thing->a_object( \@objects );
123 $thing->h_int( \%ints );
124 $thing->h_str( \%strs );
125 $thing->h_fh( \%fhs );
126 $thing->h_object( \%objects );
127 },
128 }
129);
130