Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Esteemed Monks,

I like the declarative syntax of Moose. I don't like the 728% performance hit I get compared to using a simple blessed hashref. Even Mo, minimal as it is, offers only a 27% increase in speed compared to Moose.

Benchmark Results
Benchmark: timing 1000000 iterations of blessed_hashref, hashref, mo, moose...
blessed_hashref:  1.80017 wallclock secs ( 1.79 usr +  0.00 sys =  1.79 CPU) @ 558659.22/s (n=1000000)
        hashref:  1.37839 wallclock secs ( 1.38 usr +  0.00 sys =  1.38 CPU) @ 724637.68/s (n=1000000)
             mo: 11.6823  wallclock secs (11.67 usr +  0.00 sys = 11.67 CPU) @  85689.80/s (n=1000000)
          moose: 14.8408  wallclock secs (14.82 usr +  0.00 sys = 14.82 CPU) @  67476.38/s (n=1000000)

                    Rate        moose            mo blessed_hashref      hashref
moose            67476/s           --          -21%            -88%         -91%
mo               85690/s          27%            --            -85%         -88%
blessed_hashref 558659/s         728%          552%              --         -23%
hashref         724638/s         974%          746%             30%           --
Benchmark Script:
#!/usr/bin/perl -w package MooseState; use Moose; has 'name' => ( is => 'ro', isa => 'Str', required => 1, ); has 'capital' => ( is => 'ro', isa => 'Str', required => 1, ); has 'population' => ( is => 'rw', isa => 'Int', required => 1, ); __PACKAGE__->meta->make_immutable(); package MoState; use Mo qw(required); has 'name' => ( is => 'ro', isa => 'Str', required => 1, ); has 'capital' => ( is => 'ro', isa => 'Str', required => 1, ); has 'population' => ( is => 'rw', isa => 'Int', required => 1, ); package main; use strict; use warnings 'all'; use Benchmark qw( :all :hireswallclock ); my %args = ( name => 'Colorado', capital => 'Denver', population => 5_000_000, ); my $results = timethese(1_000_000, { blessed_hashref => \&blessed_hashref, hashref => \&hashref, moose => \&moose, mo => \&mo, }); cmpthese($results); sub blessed_hashref { my $state = bless { %args }, 'Foo'; }# end blessed_hashref() sub hashref { my $state = { %args }; }# end hashref() sub moose { my $state = MooseState->new( %args ); }# end moose() sub mo { my $state = MoState->new( %args ); }# end mo()

Is there something that can be done to make constructing Moose/Mo objects much, much faster?

Update

I see now that this is a stacked comparison in that my benchmark script is having Moose do more work than it should (checking isa and required fields). As Your Mother pointed out, Mouse::XS may just be the ticket, as it's Moosey and still very fast.


In reply to The speed of blessed hashrefs, the power of Moose - how to get both? by jdrago999

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2023-10-03 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?