in reply to Time efficiency of object creation in Perl (or the "bless" function)
You have a benchmark showing very little cost for blessing objects, but the actual cost of a simple getter compared with a hash access may be the thing of concern. Well, probably it isn't. It certainly isn't the more than 144,000% hit others have suggested. The following benchmark result indicates at worst a factor of five cost between a getter and a hash access used in a simple assignment:
Rate derived baseobj hash derived 1641/s -- -0% -79% baseobj 1649/s 0% -- -79% hash 7876/s 380% 378% --
#!/usr/bin/perl use strict; use warnings; package ClassBase; sub new { my ($class, $hash) = @_; return bless $hash, $class; } sub getter { my ($self) = @_; return $self->{1}; } package Derived; use parent -norequire => 'ClassBase'; sub new { my ($class, $hash) = @_; return $class->SUPER::new ($hash); } package main; use Benchmark qw(cmpthese); my %hash = 1 .. 10; my $baseobj = ClassBase->new (\%hash); my $derived = Derived->new (\%hash); my $dump; cmpthese(-1, { baseobj => sub {$dump = $baseobj->getter() for 1 .. 1000}, derived => sub {$dump = $derived->getter() for 1 .. 1000}, hash => sub {$dump = $hash{1} for 1 .. 1000}, } );
|
|---|