|
Posted by david on March 17, 2008, 4:41 am
Please log in for more thread options
> 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++;
> $self++;
> $self++;
> $self++;
> $self++;
> $self++;
> $self++;
> $self++;
> $self++;
> $self++;
> 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++;
> $field1++;
> $field2++;
> $field3++;
> $field4++;
> $field5++;
> $field6++;
> $field7++;
> $field8++;
> $field9++;
> 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 think that the point of inside-out objects is not to make OO safer
and not faster, The real question is why do you need to make perl oo
faster. In most cases it is fast enough. There are pathological cases
where you have to construct billions of objects. In this case you may
choose a non oo solution (This is also true for compiled languages).
The most important lesson I learned as programmer is "don;t optimize,
profile". It can be that the bottle neck is in a place you never
thought.
Best regards,
David
P.S. In insisde out object you have to write a destructor to clean the
hashes
|