Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: inside-out objects using arrays?

by rvosa (Curate)
on Sep 18, 2005 at 18:11 UTC ( #493040=note: print w/replies, xml ) Need Help??

in reply to inside-out objects using arrays?

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://493040]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2018-07-20 05:21 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (424 votes). Check out past polls.