Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
Perl-Sensitive Sunglasses
 
PerlMonks  

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 http://search.cpan.org/~rvosa/Bio-Phylo-0.04/lib/Bio/Phylo/Trees/Node.pm (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 } });
Result:
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

Title:
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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (13)
    As of 2014-04-16 20:49 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (434 votes), past polls