K
Koszalek Opalek
I was about to redesign my class to use inside-out objects.
I was hoping that except for a cleaner design it will also result in
some performance boost.
(I thought that 10 hashes with approx. 1000 keys will be faster than
1000 hashes with 10 keys.)
However, a simple experiment reveals that the opposite is true.
Inside-out objects are approximately 3 times slower in this example --
and it gets worse as the number of object grows.
bash-3.2$ time ./makeregularobj.pl
real 0m0.156s
user 0m0.093s
sys 0m0.000s
bash-3.2$ time ./makeinsideoutobj.pl
real 0m0.437s
user 0m0.358s
sys 0m0.015s
I attach the two files below. Any comments?
Apart from inside-out objects what other techniques could be used to
accelerate OO Perl?
I looked at the fields module but it has been removed from Perl 5.10.
#------ makeregularobj.pl
#!/usr/bin/perl
use strict;
my $no_obj = $ARGV[0] || 10_000;
{
package P;
sub new
{
my $_class = shift;
my %self;
$self{field0}++;
$self{field1}++;
$self{field2}++;
$self{field3}++;
$self{field4}++;
$self{field5}++;
$self{field6}++;
$self{field7}++;
$self{field8}++;
$self{field9}++;
bless \%self, $_class;
}
};
my @objs;
for (1 .. $no_obj) {
push @objs, P->new();
};
print "Created $no_obj objects (blessed hashes), data stored in ten
fields inside a hash.\n";
#------ makeinsideoutobj.pl
#!/usr/bin/perl
use strict;
my $no_obj = $ARGV[0] || 10_000;
{
my %field0;
my %field1;
my %field2;
my %field3;
my %field4;
my %field5;
my %field6;
my %field7;
my %field8;
my %field9;
{
package P;
sub new
{
my $_class = shift;
my $self = 1;
$field0{\$self}++;
$field1{\$self}++;
$field2{\$self}++;
$field3{\$self}++;
$field4{\$self}++;
$field5{\$self}++;
$field6{\$self}++;
$field7{\$self}++;
$field8{\$self}++;
$field9{\$self}++;
bless \$self, $_class;
};
};
};
P->import();
my @objs;
for (1 .. $no_obj) {
push @objs, P->new();
};
print "Created $no_obj objects (blessed scalars), data stored in ten
inside-out hashes\n";
I was hoping that except for a cleaner design it will also result in
some performance boost.
(I thought that 10 hashes with approx. 1000 keys will be faster than
1000 hashes with 10 keys.)
However, a simple experiment reveals that the opposite is true.
Inside-out objects are approximately 3 times slower in this example --
and it gets worse as the number of object grows.
bash-3.2$ time ./makeregularobj.pl
real 0m0.156s
user 0m0.093s
sys 0m0.000s
bash-3.2$ time ./makeinsideoutobj.pl
real 0m0.437s
user 0m0.358s
sys 0m0.015s
I attach the two files below. Any comments?
Apart from inside-out objects what other techniques could be used to
accelerate OO Perl?
I looked at the fields module but it has been removed from Perl 5.10.
#------ makeregularobj.pl
#!/usr/bin/perl
use strict;
my $no_obj = $ARGV[0] || 10_000;
{
package P;
sub new
{
my $_class = shift;
my %self;
$self{field0}++;
$self{field1}++;
$self{field2}++;
$self{field3}++;
$self{field4}++;
$self{field5}++;
$self{field6}++;
$self{field7}++;
$self{field8}++;
$self{field9}++;
bless \%self, $_class;
}
};
my @objs;
for (1 .. $no_obj) {
push @objs, P->new();
};
print "Created $no_obj objects (blessed hashes), data stored in ten
fields inside a hash.\n";
#------ makeinsideoutobj.pl
#!/usr/bin/perl
use strict;
my $no_obj = $ARGV[0] || 10_000;
{
my %field0;
my %field1;
my %field2;
my %field3;
my %field4;
my %field5;
my %field6;
my %field7;
my %field8;
my %field9;
{
package P;
sub new
{
my $_class = shift;
my $self = 1;
$field0{\$self}++;
$field1{\$self}++;
$field2{\$self}++;
$field3{\$self}++;
$field4{\$self}++;
$field5{\$self}++;
$field6{\$self}++;
$field7{\$self}++;
$field8{\$self}++;
$field9{\$self}++;
bless \$self, $_class;
};
};
};
P->import();
my @objs;
for (1 .. $no_obj) {
push @objs, P->new();
};
print "Created $no_obj objects (blessed scalars), data stored in ten
inside-out hashes\n";