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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Indeed; writing all those getters and setters manually is inconvenient.

use strict; use warnings; use Benchmark qw(cmpthese); use Time::Limit qw(20); package MOOSE { use Moose; has a => ( reader => 'get_a', writer => 'set_a', isa => 'Int', traits => ['Counter'], handles => { adjust_a => 'inc' }, ); has b => ( reader => 'get_b', writer => 'set_b', isa => 'Int', ); has c => ( reader => 'get_c', writer => 'set_c', isa => 'Int', ); __PACKAGE__->meta->make_immutable; } package OO { use Scalar::Util qw( looks_like_number ); sub new { bless $_[1], $_[0] } sub get_a{ my( $o ) = @_; $o->{a} }; sub set_a{ my( $o, $v ) = @_; $o->{a} = $v };; sub get_b{ my( $o ) = @_; $o->{b} }; sub set_b{ my( $o, $v ) = @_; $o->{b} = $v };; sub get_c{ my( $o ) = @_; $o->{c} }; sub set_c{ my( $o, $v ) = @_; $o->{c} = $v };; sub adjust_a{ my( $o ) = @_; $o->set_a( $o->get_a() + 1 ) }; } my @AoH1 = map 'OO'->new({ a=>1, b=>2, c=>3 }), 1 .. 3e4; my @AoH2 = map 'MOOSE'->new({ a=>1, b=>2, c=>3 }), 1 .. 3e4; cmpthese -3, { OO => sub { $_->adjust_a for @AoH1 }, MOOSE => sub { $_->adjust_a for @AoH2 }, };
Rate OO MOOSE OO 3.47/s -- -47% MOOSE 6.56/s 89% --
use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

In reply to Re^2: Perl object memory overhead by tobyink
in thread Perl object memory overhead by Ineffectual

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?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-25 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found