Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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 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.


Comment on Re: inside-out objects using arrays?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://493040]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-09-16 12:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (14 votes), past polls