Re: hash of objects inside an instance

[email protected] (Erik Colson)
Newsgroups perl.moose
Message-ID <[email protected]>
On 29 Nov 2011, at 09:40, Tomas Doran wrote:

> 
> On 29 Nov 2011, at 00:29, Erik Colson wrote:
> 
>> This creates a lot of overhead by creating all the necessary objects.
>> I implemented it using a EmployeeDepartment class as would be the case when using RDBS. It works well, but I hope the app won't get too slow because of this...
> 
> Do you actually _know_ that this is a lot of overhead in real terms?
> 
> I.e. do you have profile results to show that this is the slowest part?
> 
> Otherwise I call premature optimisation.
> 
> Cheers
> t0m
> 


Here's some data. The testscript is added below. Results are as expected...

Employee1 is with using a supplementary class.
Employee2 is with using a hash where the keys are the strignification of the department objects.

Employee1 creation elapsed time: 4.398077 seconds!
Employee2 creation elapsed time: 2.870149 seconds!
Employee1 retrieve elapsed time: 0.297852 seconds!
Employee2 retrieve elapsed time: 0.25303 seconds!


######################################################################################
package Employee1;

use Moose;

has 'ID' => ( is => 'ro', isa => 'Num' );
has 'a' => ( is      => 'ro',
             isa     => 'ArrayRef[EmployeeDepartment]',
             traits  => ['Array'],
             default => sub { [] },
             handles => { add_employeedepartment  => 'push',
                          find_employeedepartment => 'first',
                          all_employeedepartments => 'elements'
             }
);

no Moose;

__PACKAGE__->meta->make_immutable;

1;

##################################################################################

package EmployeeDepartment;
use Moose;
has 'department' => ( is => 'ro', isa => 'Department' );
has 'percentage' => ( is => 'rw', isa => 'Num' );

no Moose;
__PACKAGE__->meta->make_immutable;
1;

##################################################################################
package Department;
use Moose;
has 'ID' => ( is => 'ro', isa => 'Num' );
no Moose;
__PACKAGE__->meta->make_immutable;
1;
##################################################################################

package Employee2;
use Moose;

has 'ID' => ( is => 'ro', isa => 'Num' );
has 'h' => ( is => 'ro', isa => 'HashRef', default => sub { {} } );

no Moose;

__PACKAGE__->meta->make_immutable;

1;

##################################################################################
package main;

use strict;
use warnings;

use Time::HiRes;

my @employee1instances;
my @employee2instances;

######################################### create some data

my $start = [ Time::HiRes::gettimeofday() ];

for my $i ( 1 .. 40000 ) {
    my $e = Employee1->new( ID => $i );
    $e->add_employeedepartment(
                      EmployeeDepartment->new(
                          department => Department->new( ID => $i * 10 + $_ ),
                          percentage => rand(100)
                      )
    ) for ( 1 .. 5 );
    push @employee1instances, $e;
}

my $elapsed = Time::HiRes::tv_interval($start);
print "Employee1 creation elapsed time: $elapsed seconds!\n";

$start = [ Time::HiRes::gettimeofday() ];

for my $i ( 40001 .. 80000 ) {
    my $e = Employee2->new( ID => $i );
    $e->h->{ Department->new( ID => $i * 10 + $_ ) } = rand(100)
        for ( 1 .. 5 );
    push @employee2instances, $e;
}

$elapsed = Time::HiRes::tv_interval($start);
print "Employee2 creation elapsed time: $elapsed seconds!\n";


######################################### getting data back

$start = [ Time::HiRes::gettimeofday() ];

foreach my $e (@employee1instances) {
    my $total = 0;
    foreach my $ed ( $e->all_employeedepartments ) {
        $total += $ed->percentage();
    }

    # print $e->ID." ".$total."\n";
}
$elapsed = Time::HiRes::tv_interval($start);
print "Employee1 retrieve elapsed time: $elapsed seconds!\n";

$start = [ Time::HiRes::gettimeofday() ];

foreach my $e (@employee2instances) {
    my $total = 0;
    foreach my $d ( keys $e->h ) {
        $total += $e->h->{$d};
    }

    # print $e->ID." ".$total."\n";
}

$elapsed = Time::HiRes::tv_interval($start);
print "Employee2 retrieve elapsed time: $elapsed seconds!\n";
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.