in reply to Re: Faster indexing an array
in thread Faster indexing an array
Thanks a lot.
Davidos solution is faster than mine, and LanX' and CountZeros is the fastest.
For some reason Devel::NYTProf gives better results for C-style loops over 'for my $i (...)'.
The hashref is a relict from having this part in an sub, then refactored down, then inlined, now 1-lined. See here the same logic in Algorithm::Diff:
sub _withPositionsOfInInterval { my $aCollection = shift; # array ref my $start = shift; my $end = shift; my $keyGen = shift; my %d; my $index; for ( $index = $start ; $index <= $end ; $index++ ) { my $element = $aCollection->[$index]; my $key = &$keyGen( $element, @_ ); if ( exists( $d{$key} ) ) { unshift ( @{ $d{$key} }, $index ); } else { $d{$key} = [$index]; } } return wantarray ? %d : \%d; }
Having an arrayref comes from the two input-parameters, sequences X, Y or sometimes also called A, B in the descriptions of diff/LCS/align-algorithms.
Helmut Wollmersdorfer
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Faster indexing an array
by LanX (Saint) on Sep 19, 2014 at 22:34 UTC | |
|
Re^3: Faster indexing an array
by LanX (Saint) on Sep 19, 2014 at 23:17 UTC | |
by wollmers (Scribe) on Sep 20, 2014 at 06:54 UTC |