tags:

views:

52

answers:

0

Before, I had defined my elements entry as just Array in H::S because I did not need a secondary sort.

After implementing a secondary sort and a function for defining the elements to insert array references into a heap I made, the runtime of my insertions increased (alot!). This is covered in the documentation for Heap::Simple under http://search.cpan.org/perldoc?Heap::Simple#Any

However I have a hard time understanding the drawbacks of using the Any element type. Will I be creating extra array references? Should I be using key_insert to decrease runtime?

Here is the code as it currently stands:

my $heap = Heap::Simple->new( order     => \&by_num_or_str,
                              elements  => [Function => \&first_two_slots]
                             );

sub by_num_or_str
{
    my ( $a, $b ) = @_;

    my $result = 
        $b->[0] <=> $a->[0]  #0-th element is a number
                ||
        $a->[1] cmp $b->[1]; #1-st element is a string

    return $result == -1;
}

sub first_two_slots 
{ 
    my $array_ref = shift; 
    return [ @$array_ref[0,1] ]; 
}

Context: http://stackoverflow.com/questions/3151415/inserting-array-references-into-perl-heap