#! perl -sw use strict; package Things1; sub new { my $class = shift; my $self = { _thing=>0 }; return bless $self, $class; } sub get_it { my $self = shift; return $self->{_thing}; } sub set_it { my $self = shift; my $value = shift; return $self->{_thing} = $value; } 1; package Things2; sub new { my ($class) = @_; my $self = { _thing=>0 }; return bless $self, $class; } sub get_it { my ($self) = @_; return $self->{_thing}; } sub set_it { my ($self, $value) = @_; return $self->{_thing} = $value; } 1; package Things3; sub new { return bless { _thing=>0 }, $_[0]; } sub get_it { return $_[0]->{_thing}; } sub set_it { return $_[0]->{_thing} = $_[1]; } 1; package Things4; use constant CLASS => 0; use constant SELF => 0; use constant VALUE => 1; sub new { return bless { _thing=>0 }, $_[CLASS]; } sub get_it { return $_[SELF]->{_thing}; } sub set_it { return $_[SELF]->{_thing} = $_[VALUE]; } 1; package main; use strict; use vars qw/$COUNT $RUNS/; use Benchmark qw/cmpthese/; $\=$/; print $COUNT,,$RUNS; my $thing1 = new Things1; print $thing1; print "Value:", $thing1->get_it(); $thing1->set_it(99999); print "Value:", $thing1->get_it(); my $thing2 = new Things2; print $thing2; print "Value:", $thing2->get_it(); $thing2->set_it(99999); print "Value:", $thing2->get_it(); my $thing3 = new Things3; print $thing3; print "Value:", $thing3->get_it(); $thing3->set_it(99999); print "Value:", $thing3->get_it(); my $thing4 = new Things4; print $thing4; print "Value:", $thing4->get_it(); $thing4->set_it(99999); print "Value:", $thing4->get_it(); cmpthese( $::RUNS, { shift => sub{ my $thing = new Things1; $thing->set_it( 1 + $thing->get_it() ) for 1 .. $::COUNT; }, assign => sub{ my $thing = new Things2; $thing->set_it( 1 + $thing->get_it() ) for 1 .. $::COUNT; }, direct => sub{ my $thing = new Things3; $thing->set_it( 1 + $thing->get_it() ) for 1 .. $::COUNT; }, constant=> sub{ my $thing = new Things4; $thing->set_it( 1 + $thing->get_it() ) for 1 .. $::COUNT; }, } ); __END__ c:\test>212101 -COUNT=10000 -RUNS=10 1000010 Things1=HASH(0x1bdf190) Value:0 Value:99999 Things2=HASH(0x1bdf1b4) Value:0 Value:99999 Things3=HASH(0x1bd51c8) Value:0 Value:99999 Things4=HASH(0x1bd51f8) Value:0 Value:99999 Benchmark: timing 10 iterations of assign, constant, direct, shift ... assign: 4 wallclock secs ( 3.64 usr + 0.00 sys = 3.64 CPU) @ 2.75/s (n=10) constant: 3 wallclock secs ( 3.18 usr + 0.00 sys = 3.18 CPU) @ 3.15/s (n=10) direct: 3 wallclock secs ( 3.18 usr + 0.00 sys = 3.18 CPU) @ 3.14/s (n=10) shift: 4 wallclock secs ( 3.96 usr + 0.00 sys = 3.96 CPU) @ 2.53/s (n=10) Rate shift assign direct constant shift 2.53/s -- -8% -20% -20% assign 2.75/s 9% -- -12% -13% direct 3.14/s 24% 14% -- -0% constant 3.15/s 25% 14% 0% -- c:\test>