Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I ran some benchmarks, which might be of interest. I compared the node object for (actually, for the next version, with a different namespace, but the guts are the same), which uses an anonymous hashref, to the one below:
package Outside::In; use strict; use warnings; use Carp; use Scalar::Util qw(looks_like_number); use overload '""' => \&to_string; { my @name; my @parent; my @first_daughter; my @last_daughter; my @next_sister; my @previous_sister; my @branch_length; my @reclaim; my $obj_counter = 0; sub new { my $class = shift; my $instance_counter = 0; if ( @reclaim ) { @reclaim = sort { $a <=> $b } @reclaim; $instance_counter = shift(@reclaim); } else { $instance_counter = $obj_counter; $obj_counter++; } my $self = \$instance_counter; bless $self, $class; return $self; } sub set_parent { $parent[${$_[0]}] = $_[1] if $_[1]; return $_[0]; } sub get_parent { return $parent[${$_[0]}]; } sub to_string { return 'ObjectID = ' . ${$_[0]}; } sub DESTROY { delete $name[${$_[0]}]; delete $parent[${$_[0]}]; delete $first_daughter[${$_[0]}]; delete $last_daughter[${$_[0]}]; delete $next_sister[${$_[0]}]; delete $previous_sister[${$_[0]}]; delete $branch_length[${$_[0]}]; push(@reclaim, ${$_[0]}); } } package main; use strict; use warnings; use Benchmark qw(:all); use Bio::Phylo::Forest::Node; use Data::Dumper; my $oldnode1 = Bio::Phylo::Forest::Node->new; my $oldnode2 = Bio::Phylo::Forest::Node->new; $oldnode1->set_parent($oldnode2); my $newnode1 = Outside::In->new; my $newnode2 = Outside::In->new; $newnode1->set_parent($newnode2); cmpthese (1000000, { 'old' => sub { $oldnode1->get_parent }, 'new' => sub { $newnode1->get_parent } });
Rate old new old 477555/s -- -34% new 723066/s 51% --
The salient point is that:
sub get_parent { return $_[0]->{'PARENT'}; }
seems quite a bit slower than:
sub get_parent { return $parent[${$_[0]}]; }
I've heard that array look-ups are faster than hash look-ups, but I thought the difference was smaller than it is.

In reply to Re: inside-out objects using arrays? by rvosa
in thread inside-out objects using arrays? by rvosa

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (6)
    As of 2018-06-18 08:11 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (109 votes). Check out past polls.