It Works, *AND* Its Fast(er)
[gitmo/Moose.git] / benchmarks / type_constraints.pl
CommitLineData
43123819 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Benchmark qw[cmpthese];
7
7623f774 8=pod
9
10This benchmark compares the overhead of a
11auto-created type constraint vs. none at
12all vs. a custom-created type.
13
14=cut
15
43123819 16{
17 package Foo;
18 use Moose;
19 use Moose::Util::TypeConstraints;
20
21 has 'baz' => (is => 'rw');
22 has 'bar' => (is => 'rw', isa => 'Foo');
03aac1fa 23 #has 'boo' => (is => 'rw', isa => type 'CustomFoo' => where { blessed($_) && $_->isa('Foo') });
43123819 24}
25
8ecb1fa0 26{
27 package Bar;
28
29 sub new { bless {} => __PACKAGE__ }
30 sub bar {
31 my $self = shift;
32 $self->{bar} = shift if @_;
33 $self->{bar};
34 }
35}
36
43123819 37my $foo = Foo->new;
8ecb1fa0 38my $bar = Bar->new;
43123819 39
40cmpthese(200_000,
41 {
8ecb1fa0 42 'hand coded' => sub {
43 $bar->bar($bar);
44 },
43123819 45 'w/out_constraint' => sub {
46 $foo->baz($foo);
47 },
48 'w_constraint' => sub {
49 $foo->bar($foo);
50 },
03aac1fa 51 #'w_custom_constraint' => sub {
52 # $foo->boo($foo);
53 #},
43123819 54 }
55);
56
571;