in reply to Re: Help tightening up a subroutine please
in thread Help tightening up a subroutine please

Your code here:
@{$sets{$fasta_id}[$setscounter]{$sitekey}} = (); foreach ( sort @{$matches{$fasta_id}{$sitekey}} ) { last if $_ > $upperlimit; next if $_ < $lowerlimit; push @{$sets{$fasta_id}[$setscounter]{$sitekey}}, $_;

Looks a lot like my original code, in the first bit, here:
for my $hit (@{$matches{$fasta_id}{$sitekey}}) { next unless ($hit >= $lowerlimit); last unless ($hit <= $upperlimit); my $ggg = $hit + 0; push (@arrayA, $ggg);

And indeed, it does have a speed advantage. I don't need the sort, as the elements are already sorted in numerical order, so I can (and do) leave that out. As for knowing thy data, this here's the tricky part. I am going through the numbers in the arrays, and basically setting aside clusters of EVERY group of numbers $span distance apart. Starting from the lowest element, and proceeding all the way to the highest. So, how many elements there are above $upperlimit is as variable as possible. It goes from all to none.

Thanks for the input, though.
Matt

Replies are listed 'Best First'.
Re^3: Help tightening up a subroutine please
by radiantmatrix (Parson) on Jan 24, 2007 at 22:16 UTC

    Hm, somehow I missed that. Given the extra bit of data you supplied, I might recommend the use of List::MoreUtil's firstidx and lastidx functions. The fact that it's XS ought to help with the speed.

    # for clarity... my $result; my $src = $matches{$fasta_id}{$sitekey}; # Use a slice and firstidx/lastidx to copy the range of values @newArray = @$src[ (firstidx {$_ >= $lowerlimit} @$src )..(lastidx {$_ <= $upperlimit} + @$src) ]; $result = \@newArray;
    <radiant.matrix>
    Ramblings and references
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet