Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

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

I read the following as array-based objects are 20% faster and 70% 40% smaller than the hash based equivalent for little extra effort and no loss of clarity.

#! perl -slw use strict; package ArrayBased; use constant { CLASS => 0, SELF => 0, FIRST => 0, SECOND => 1, THIRD => 2, FOURTH => 3, FIFTH => 5, }; sub new { return bless [ qw[ one two three four five ] ], $_[CLASS]; } sub method { (undef) = $_[SELF][FIRST]; (undef) = $_[SELF][SECOND]; (undef) = $_[SELF][THIRD]; (undef) = $_[SELF][FOURTH]; (undef) = $_[SELF][FIFTH]; $_[SELF][FIRST] = 1; $_[SELF][SECOND]= 2; $_[SELF][THIRD] = 3; $_[SELF][FOURTH]= 4; $_[SELF][FIFTH] = 5; } package HashBased; sub new { my( $class ) = @_; return bless { FIRST => 'one', SECOND => 'two', THIRD => 'three', FOURTH => 'four', FIFTH => 'five', }, $class; } sub method { my( $self ) = @_; (undef) = $self->{FIRST}; (undef) = $self->{SECOND}; (undef) = $self->{THIRD}; (undef) = $self->{FOURTH}; (undef) = $self->{FIFTH}; $self->{FIRST} = 1; $self->{SECOND} = 2; $self->{THIRD} = 3; $self->{FOURTH} = 4; $self->{FIFTH} = 5; } package main; use Benchmark qw[ cmpthese ]; use Devel::Size qw[ size total_size ]; cmpthese -1, { arrayBased => q[ my $o = new ArrayBased; $o->method; ], hashBased => q[ my $o = new HashBased; $o->method; ], }; print "ArrayBased bytes: ", total_size( ArrayBased->new() ); print " HashBased bytes: ", total_size( HashBased->new() ); __END__ P:\test>462336 Rate hashBased arrayBased hashBased 48505/s -- -16% arrayBased 57853/s 19% -- ArrayBased bytes: 220 HashBased bytes: 373

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

In reply to Re^4: Which, if any, is faster? (20% faster/ 40% smaller) by BrowserUk
in thread Which, if any, is faster? by rvosa

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 chilling in the Monastery: (5)
As of 2024-03-28 08:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found