package Tiny;
# Filename Tiny.pm
# The Worlds Smallest Perl Object
use strict;
sub new {
return bless { }, shift;
}
1;
-------------------------------------------------------
package NotSoTiny;
# Filename NotTiny.pm
use strict;
sub new {
return bless { value => 'A value' }, shift;
}
sub get_value {
return shift->{value};
}
1;
------------------------------------------------------
#! /usr/bin/perl -w
#Filname: bench
use Benchmark qw(timethese); # perl 5.6 has 'cmpthese'
# which is very cool, and
# much more informative
# If you have 5.6 just
# s/timethese/cmpthese/g
use strict;
use lib ('./');
use Tiny;
use NotSoTiny;
my ( $roll, @roll );
my $loop = 250000; # You will want this var up here to
# Adjust itineratations to avoid
# warnings
###### Preload Objs ###################################
# Benchmark two ways one with pre constructed objs and
# another without considering construction costs.
my $TinyObjBase = Tiny->new();
my $NotTinyObj = NotSoTiny->new();
###### SubRoutine Vars #################################
# To be completely fair do the same with the Vars for
# the subroutines.
my $TinySub;
my $NotSoTinySub;
###### End Init ###########
system("clear");
my $loops;
for $loops ($loop) {
timethese $loops, {
##### Subroutine Comparisons
TinySubPreVar => sub {
$TinySub = Tiny();
},
TinySubNewVar => sub {
my $NewTinySub = Tiny();
},
NotSoTinySubPreVar => sub {
$NotSoTinySub = NotSoTinySub();
},
NotSoTinySubNewVar => sub {
my $NewNotSoTinySub = NotSoTinySub();
},
##### Object Comparisons
TinyObjReused => sub {
$TinyObjBase;
},
TinyObjNew => sub {
my $NewTinyObj = Tiny->new();
},
NotSoTinyObjReused => sub {
$TinySub = $NotTinyObj->get_value();
},
NotSoTinyObjNew => sub {
my $NotSoTinyObjNew = NotSoTiny->new();
$TinySub = $NotSoTinyObjNew->get_value();
},
};
}
sub Tiny {
return;
}
sub NotSoTinySub {
return 'A value';
}
|