Skip to content

Instantly share code, notes, and snippets.

@Ovid

Ovid/bench.pl Secret

Last active Mar 1, 2021
Embed
What would you like to do?
Object::Pad v Moo v Moose v Core
#!/usr/bin/env perl
# OS is darwin
# Perl version is v5.26.2
# Moose version is 2.2014
# Moo version is 2.004004
# Object::Pad version is 0.36
# Rate Moose w/constructor Object::Pad w/constructor Core w/constructor Moose w/o constructor Core w/o constructor Moo w/constructor Object::Pad w/o constructor Moo w/o constructor
# Moose w/constructor 149701/s -- -19% -26% -27% -32% -34% -50% -55%
# Object::Pad w/constructor 185185/s 24% -- -8% -10% -16% -18% -39% -44%
# Core w/constructor 201613/s 35% 9% -- -2% -8% -10% -33% -40%
# Moose w/o constructor 204918/s 37% 11% 2% -- -7% -9% -32% -39%
# Core w/o constructor 219298/s 46% 18% 9% 7% -- -3% -27% -34%
# Moo w/constructor 225225/s 50% 22% 12% 10% 3% -- -25% -32%
# Object::Pad w/o constructor 301205/s 101% 63% 49% 47% 37% 34% -- -10%
# Moo w/o constructor 333333/s 123% 80% 65% 63% 52% 48% 11% --
use strict;
use warnings;
use Benchmark qw(:all);
use 5.26.0;
package CustomerCore {
sub new {
my ( $class, %args ) = @_;
bless \%args => $class;
}
sub title {
my $self = shift;
if (@_) {
$self->{title} = shift;
}
else {
return $self->{title};
}
}
sub name {
my $self = $_[0];
return $self->title
? return ( join ' ' => $self->title, $self->{name} )
: $self->{name};
}
}
package CustomerMoose {
use Moose;
has 'title' => ( is => 'rw' );
has '_name' => ( is => 'ro', init_arg => 'name' );
sub name {
my $self = $_[0];
return $self->title
? return ( join ' ' => $self->title, $self->_name )
: $self->_name;
}
__PACKAGE__->meta->make_immutable;
}
package CustomerMoo {
use Moo;
has 'title' => ( is => 'rw' );
has '_name' => ( is => 'ro', init_arg => 'name' );
sub name {
my $self = $_[0];
return $self->title
? return ( join ' ' => $self->title, $self->_name )
: $self->_name;
}
}
use Object::Pad;
class Customer {
has $title : writer;
has $name;
BUILD(%arg_for) {
$title = $arg_for{title};
$name = $arg_for{name};
}
method name() {
return $title ? "$title $name" : $name;
}
}
say "OS is " . $^O;
say "Perl version is " . $^V;
say "Moose version is " . Moose->VERSION;
say "Moo version is " . Moo->VERSION;
say "Object::Pad version is " . Object::Pad->VERSION;
cmpthese(
250_000,
{
'Object::Pad w/o constructor' => sub {
state $cust = Customer->new( title => 'Mr.', name => 'Ovid' );
run_example( $cust, 'set_title' );
},
'Moo w/o constructor' => sub {
state $cust = CustomerMoo->new( title => 'Mr.', name => 'divO' );
run_example( $cust, 'title' );
},
'Moose w/o constructor' => sub {
state $cust
= CustomerMoose->new( title => 'Mr.', name => 'divO' );
run_example( $cust, 'title' );
},
'Core w/o constructor' => sub {
state $cust = CustomerCore->new( title => 'Mr.', name => 'divO' );
run_example( $cust, 'title' );
},
'Object::Pad w/constructor' => sub {
my $cust = Customer->new( title => 'Mr.', name => 'Ovid' );
run_example( $cust, 'set_title' );
},
'Moo w/constructor' => sub {
my $cust = CustomerMoo->new( title => 'Mr.', name => 'divO' );
run_example( $cust, 'title' );
},
'Moose w/constructor' => sub {
my $cust = CustomerMoose->new( title => 'Mr.', name => 'divO' );
run_example( $cust, 'title' );
},
'Core w/constructor' => sub {
my $cust = CustomerCore->new( title => 'Mr.', name => 'divO' );
run_example( $cust, 'title' );
},
}
);
sub run_example {
my ( $cust, $set_title ) = @_;
for ( 1 .. 3 ) {
$cust->$set_title('');
$cust->name;
$cust->$set_title('Mr.');
$cust->name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment