in reply to Re: Abusing Map (Corrected second code block)
in thread Abusing Map
Given,
my @b; $b[ $_-1 ] = $a[ $_-1 ] - $a[ $_ ] for 1 .. $#a;
Micro-optimized:
my @b; $b[ $_ ] = $a[ $_ ] - $a[ $_+1 ] for 0 .. $#a-1;
Micro-optimized further:
my @b = @a; $b[ $_ ] -= $b[ $_+1 ] for 0 .. $#b-1; pop @b;
Well, I suspect those are faster. I didn't actually test.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Abusing Map (Corrected second code block)
by hippo (Bishop) on May 18, 2018 at 08:59 UTC | |
Well, I suspect those are faster. I didn't actually test. Update: Thanks for everyone who's been following along, especially Eily who pointed out the conceptual error with the string evals. Here then is a hopefully correct version (including BrowserUK's optimised "while" solution) using anon subs which has somewhat more believable figures (Perl is fast, just not that fast :-)
Producing this output:
So the "while" approach does just win out in the end. The rest of this post is all cobblers. Upon close inspection I've not set up the arrays correctly. It's the cardinal sin of using @a and @b transliterated from the snippets posted. Mea cupla. See above for the updated script and figures. I was intrigued, so I did test.
With these results:
Here's the proper, working code:
Giving these figures:
Oddly, this doesn't afect the qualitative results noticeably. ike2 (with the pop) is still consistently the slowest. The other two are roughly the same to within the detection levels of the test but ike1 may be a smidge faster for large arrays. | [reply] [d/l] [select] |
by BrowserUk (Patriarch) on May 18, 2018 at 13:38 UTC | |
You could add the while version to the benchmark; it was the only one I promoted for efficiency. With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice.
Suck that fhit
| [reply] |
by hippo (Bishop) on May 18, 2018 at 14:19 UTC | |
Ignore this - it's the same error as the GP, sorry. I've now modified the script in the previous post to include this as the fourth comparison. It does look to be the most efficient as you suspected. I've just tried that but it errors out with Modification of non-creatable array value attempted, subscript -2 at (eval 11) line 1.. Here's the code:
I'm not sure how it manages to get down to -2. Have I mis-typed your "while" version? | [reply] [d/l] [select] |
by BrowserUk (Patriarch) on May 18, 2018 at 15:38 UTC |