Jonathan,

I tested your benchmark script against the (in progress) immutable Moose branch. Here is what I got:

First, your benchmark:

Benchmark: timing 1000 iterations of ClassAccessorPage, MoosePage...
ClassAccessorPage: 0 wallclock secs ( 0.02 usr + 0.00 sys = 0.02 CPU) @ 50000.00/s (n=1000)
           (warning: too few iterations for a reliable count)
MoosePage: 22 wallclock secs (18.41 usr + 0.33 sys = 18.74 CPU) @ 53.36/s (n=1000)


Then my benchmark before making the class immutable (I have a 1.5gHz G4 powerbook with 1.5g of RAM):

Benchmark: timing 1000 iterations of ClassAccessorPage, MoosePage...
ClassAccessorPage: 0 wallclock secs ( 0.01 usr + 0.00 sys = 0.01 CPU) @ 100000.00/s (n=1000)
            (warning: too few iterations for a reliable count)
MoosePage: 14 wallclock secs ( 9.78 usr + 0.16 sys = 9.94 CPU) @ 100.60/s (n=1000)

Now, with the class made immutable:

Benchmark: timing 1000 iterations of ClassAccessorPage, MoosePage...
ClassAccessorPage: 0 wallclock secs ( 0.01 usr + 0.00 sys = 0.01 CPU) @ 100000.00/s (n=1000)
            (warning: too few iterations for a reliable count)
MoosePage: 1 wallclock secs ( 0.93 usr + 0.01 sys = 0.94 CPU) @ 1063.83/s (n=1000)

Now, Class::Accessor is still *much* faster, but it does not check the fields in the hash passed to new, it just copies them into the instance. Moose does not do this, it only allows legitimate slots to be added to the instance. In fact, here is the constructor it generates (formatted to make it readable).

sub {
    my $class = shift;
    my %params = (scalar @_ == 1) ? %{$_[0]} : @_;
    my $instance = bless {} => $class;
    ## title
    (exists $params{'title'}) && do {
        my $val = $params{'title'};
        $instance->{'title'} = $val;
    };
    return $instance;
}

As you can see, there are still some things which can be cleaned up for minimal cases like this, like removing the lexical usage in the slot assignment. I am working on this now.

- Stevan


On Nov 12, 2006, at 2:51 PM, Jonathan Swartz wrote:

   #!/usr/bin/perl
   use Benchmark;
   use strict;
   use warnings;

   { package MoosePage;
     use Moose;

     has 'title' => (is => 'rw');
   }

   { package ClassAccessorPage;
     use strict;
     use warnings;
     use base qw(Class::Accessor);

     __PACKAGE__->mk_accessors(qw(title));
   }

printf "Using Moose version %s, Class::MOP version %s, Class::Accessor version %s\n", $Moose::VERSION, $Class::MOP::VERSION, $Class::Accessor::VERSION;

   timethese(1000, {
'MoosePage' => sub { my $page = new MoosePage(title => 'article') }, 'ClassAccessorPage' => sub { my $page = new ClassAccessorPage ({title => 'article'}) },
             });

Reply via email to