Skip to content

Instantly share code, notes, and snippets.

@danaj
Created October 3, 2011 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danaj/1259851 to your computer and use it in GitHub Desktop.
Save danaj/1259851 to your computer and use it in GitHub Desktop.
Testing Moo vs. Mouse vs. Moose with the Data::BitStream package
For package Data::BitStream. Perl 5.14.1, Moose 2.0202, Mouse 0.93, Moo 0.009011.
Steps to convert package from Mouse to Moose:
1. find . -type f|egrep '\.(t|pl|pm)$' | xargs perl -i -pe 's/Mouse/Moose/g'
Steps to convert from Mouse to Moo:
1. find . -type f|egrep '\.(t|pl|pm)$' | xargs perl -i -pe 's/Mouse/Moo/g'
2. Change all the 'with x, y, z' to 'with x; with y; with z;'
3. Comment out lines: '__PACKAGE__->meta->make_immutable;'
4. Change has commands from:
has 'pos' => (is => 'ro',
writer => '_setpos',
isa => 'Int',
default => 0);
to
has 'pos' => (is => 'ro',
writer => '_setpos',
isa => sub { $_[0] =~ /^\d+$/ }
default => sub { 0 });
Optionally remove the isa since it will cost about 20% overall performance.
5. Change lines in t/ from:
Data::BitStream::Code::Baer->meta->apply($stream);
to
Role::Tiny->apply_roles_to_object($stream, qw(Data::BitStream::Code::Baer));
Tests:
There is one test that will fail with Moo: the 'has' method is still exported
even after 'no Moo'. Everything else works identically.
Benchmarks (on a HP Z600, Xeon X5650 @ 2.67GHz, perl 5.14.1). Best result of 3-5 runs.
First let's just lump Base.pm and String.pm in one big file, remove all the
roles so we have no codes outside the basics, and create a single object.
That's it -- one file, one simple class, one instantiation. Real time.
Moose: 0.214s
Mouse: 0.040s
Moo: 0.035s
Clearly Moose is heavy even with this simple under-400-line program.
Moo and Mouse both do a good job of allowing the script to run quickly.
How about running the test suite? 22 files, 1000s of tests. 'prove -Ilib t/'
W/ isa W/out isa
Moose: 17.24s 15.15s
Mouse: 8.74s 8.66s
Moo: 12.91s 10.65s
Well, Mouse's fancy XS accessor stuff for simple isas works wonders. There's
basically no penalty for doing integer type constraints. Moo and Moose both have
a 2 second penalty. Moo is definitely faster than Moose, but Mouse easily wins.
How about something more time consuming. Gamma encode an array of 59k integers,
timing just the stream encode and decode methods (so ignore all startup and setup):
Moose: 330ms encode 372ms decode
Mouse: 268ms encode 277ms decode (1.0x)
Moo: 361ms encode 432ms decode
Hmm. At this point we're timing a big loop that calling the write and put_unary
(read/get_unary for decode) methods. NYTProf says the gets and set are the hot
spots. It says the same thing with Mouse, but the overall time taken is 1.8x
higher with Moo. 1.5x higher in write(), 1.8x higher in read(), etc.
What I find most odd is how Moo is _slower_ than Moose.
Comment from mst:
if you have Class::XSAccessor installed and have just "has foo => (is => 'ro');" you'll find Moo uses that. which will probably end up being faster. Also, try testing Mouse pure perl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment