wollmers has asked for the wisdom of the Perl Monks concerning the following question:
Dear monks,
as this part takes ~15% of an fast alignment algorithm, it would be nice to have a faster way.
my $Y = [ qw( a b a) ]; my $YPos; my $index; for ( $index = 0 ; $index <= $#$Y ; $index++ ) { push ( @{ $YPos->{$Y->[$index]} }, $index ); } # now $YPos should contain my $result = { a => [ 0, 2 ], b => [ 1 ], };
TIA (Thanks In Advance)
Helmut Wollmersdorfer
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Faster indexing an array
by CountZero (Bishop) on Sep 19, 2014 at 21:06 UTC | |
should be a bit faster as it eliminates one step of indexing. As an aside, why do you use scalars and references rather than arrays and hashes? Update: I ran your code on an array of 10 million elements. It took 27 seconds. My code finished in 20 seconds. Using arrays and hashes instead of scalars and references made it run in 19 seconds, another 5% saved! CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics | [reply] [d/l] |
by wollmers (Scribe) on Sep 19, 2014 at 22:09 UTC | |
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:
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 | [reply] [d/l] |
by LanX (Saint) on Sep 19, 2014 at 22:34 UTC | |
Cheers Rolf (addicted to the Perl Programming Language and ☆☆☆☆ :) | [reply] [d/l] [select] |
by LanX (Saint) on Sep 19, 2014 at 23:17 UTC | |
(you are welcome to do proper benchmarking =) but optimizing or inlining &$keyGen( $element, @_ ) should lead to much more efficiency, since 25% of 15% doesn't count much!!!
updateof course you could directly iterate over the values of a hash slice of an array slice... :)
BTW: @a=0..1e6;$start=1e5;$end=2*$start; Cheers Rolf (addicted to the Perl Programming Language and ☆☆☆☆ :) | [reply] [d/l] [select] |
by wollmers (Scribe) on Sep 20, 2014 at 06:54 UTC | |
by SuicideJunkie (Vicar) on Sep 19, 2014 at 21:33 UTC | |
I for one, tend to use scalars and refs because that makes using the variables more consistent (arrows for everybody!). It makes for less effort to pass around the refs to helper functions. I'll still use a direct array variable for something very local and/or temporary, but in general it'll be refs. These days, I often go all the way to a $universal hashref, which makes it almost trivial to save and restore the program state. PS: | [reply] [d/l] [select] |
by doom (Deacon) on Feb 17, 2015 at 22:23 UTC | |
(2) But the real reason I'm writing is this remark:
"These days, I often go all the way to a $universal hashref, which makes it almost trivial to save and restore the program state."
(b) but you're reinventing perl objects. You might as well just do objects... then if you're using something moose-like, like Mouse or Moo, the "allowed names" get turned into a list of "has" lines. | [reply] [d/l] |
|
Re: Faster indexing an array
by davido (Cardinal) on Sep 19, 2014 at 21:03 UTC | |
I'll let you do the benchmarking. However, I suspect that using a range-based for loop will be faster than a C-style for loop, because it pushes more work out of Perl and into perl.
If that's still not fast enough, rewrite the alignment algorithm with Inline::C. :) You're currently chasing after possibly halving the runtime of a segment of code that consumes 15% of a tight algorithm. Let's say you could completely optimize out this tranform. That's still only a 15% savings. If, instead, you're able to cut it in half, that's a 7.5% savings overall. Rewrite the entire algorithm in C using Inline::C, and you might be able to shave half off the algorithm's runtime, rather than half off of a small portion of the algorithm's runtime. Dave | [reply] [d/l] [select] |
|
Re: Faster indexing an array
by LanX (Saint) on Sep 19, 2014 at 21:05 UTC | |
please note that the debugger-shell has problems to handle lexical variables, that's why I omitted them. And I avoided dereferencing, but I don't think it'll cost you much reintroducing it. (if needed) But you should add my declarations in productive code.
Cheers Rolf (addicted to the Perl Programming Language and ☆☆☆☆ :) PS: from 5.14 on you can also write push $Ypos{$_},$i++ for @Y | [reply] [d/l] [select] |
|
Re: Faster indexing an array
by johngg (Canon) on Sep 19, 2014 at 21:32 UTC | |
Others beat me to it but here's a benchmark.
The output.
A modest increase in speed so perhaps the Inline::C suggestion will be required. Cheers, JohnGG | [reply] [d/l] [select] |
|
Re: Faster indexing an array
by LanX (Saint) on Sep 19, 2014 at 22:29 UTC | |
you should show us more of the whole algorithm. I'm pretty sure our solutions for this isolated part can't be done faster in pure Perl. But probably you just need another general tactic.
Cheers Rolf (addicted to the Perl Programming Language and ☆☆☆☆ :) | [reply] |
by wollmers (Scribe) on Sep 19, 2014 at 23:42 UTC | |
I need something similar as result as sdiff() of Algorithm::Diff provides.
The two most popular of the fastest algorithms for LCS are Hunt/McIllroy (used in Algorithm::Diff, Algorithm::LCS from BackPAN written in XS) and Meyers/Ukkonen (used in GNU-diff, String::Similarity). What I implemented is an improved Hunt/McIllroy from AFROZA BEGUM, A GREEDY APPROACH FOR COMPUTING LONGEST COMMON SUBSEQUENCES, Journal of Prime Research in Mathematics Vol. 4(2008), 165-170. It beats A::D::sdiff(). To be fair A::D provides more functionality, which I also try to strip down for comparison. In the end I would like to modify the XS of A::LCS. A::LCS processes 0.8 Mio/s in comparison to 14 thousand/s A::D::sdiff() of length=10, edit-distance=4. But making the aligned hunks via perl from the LCS of A::LCS slows down to 35 thousand/s. A::LCS-aligned 35714.29/s (n=50000) lcs_greedy_aligned: 22831.05/s (n=50000) A::D::sdiff: 14204.55/s (n=50000) Here my code (dirty as it is work in progress):
| [reply] [d/l] [select] |