#! perl -slw use strict; package ArrayBased; use constant { CLASS => 0, SELF => 0, FIRST => 0, SECOND => 1, THIRD => 2, FOURTH => 3, FIFTH => 5, }; sub new { return bless [ qw[ one two three four five ] ], $_[CLASS]; } sub method { (undef) = $_[SELF][FIRST]; (undef) = $_[SELF][SECOND]; (undef) = $_[SELF][THIRD]; (undef) = $_[SELF][FOURTH]; (undef) = $_[SELF][FIFTH]; $_[SELF][FIRST] = 1; $_[SELF][SECOND]= 2; $_[SELF][THIRD] = 3; $_[SELF][FOURTH]= 4; $_[SELF][FIFTH] = 5; } package HashBased; sub new { my( $class ) = @_; return bless { FIRST => 'one', SECOND => 'two', THIRD => 'three', FOURTH => 'four', FIFTH => 'five', }, $class; } sub method { my( $self ) = @_; (undef) = $self->{FIRST}; (undef) = $self->{SECOND}; (undef) = $self->{THIRD}; (undef) = $self->{FOURTH}; (undef) = $self->{FIFTH}; $self->{FIRST} = 1; $self->{SECOND} = 2; $self->{THIRD} = 3; $self->{FOURTH} = 4; $self->{FIFTH} = 5; } package main; use Benchmark qw[ cmpthese ]; use Devel::Size qw[ size total_size ]; cmpthese -1, { arrayBased => q[ my $o = new ArrayBased; $o->method; ], hashBased => q[ my $o = new HashBased; $o->method; ], }; print "ArrayBased bytes: ", total_size( ArrayBased->new() ); print " HashBased bytes: ", total_size( HashBased->new() ); __END__ P:\test>462336 Rate hashBased arrayBased hashBased 48505/s -- -16% arrayBased 57853/s 19% -- ArrayBased bytes: 220 HashBased bytes: 373