Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^4: Abusing Map (Corrected second code block)

by BrowserUk (Pope)
on May 18, 2018 at 13:38 UTC ( #1214847=note: print w/replies, xml ) Need Help??


in reply to Re^3: Abusing Map (Corrected second code block)
in thread Abusing Map

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
  • Comment on Re^4: Abusing Map (Corrected second code block)

Replies are listed 'Best First'.
Re^5: Abusing Map (Corrected second code block)
by hippo (Chancellor) 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:

    #!/usr/bin/env perl use strict; use warnings; use Benchmark 'cmpthese'; for my $arrsize (1e4, 1e6, 1e8) { print "Source array has $arrsize elements\n"; my @x; push @x, rand for 1..$arrsize; cmpthese (1e7, { 'BUK' => ' my @y; $y[ $_-1 ] = $x[ $_-1 ] - $x[ $_ ] for 1 .. +$#x; ', 'ike1' => ' my @y; $y[ $_ ] = $x[ $_ ] - $x[ $_+1 ] for 0 .. $ +#x-1; ', 'ike2' => ' my @y = @x; $y[ $_ ] -= $y[ $_+1 ] for 0 .. $#y-1; + pop @y; ', 'while' => ' my @y; my $i = $#x; $y[ $i ] = $x[ $i ] + $x[ --$ +i ] while $i; ', }); print '-' x 80 . "\n"; }

    I'm not sure how it manages to get down to -2. Have I mis-typed your "while" version?

      It does look to be the most efficient as you suspected.

      The really fastest version -- which is only applicable if you no longer need the original array and can modify it in place, which is often the case in my work -- is:

      my $i = 0; $x[ $i ] -= $x[ ++$i ] while $i < $#x; --$#x;

      But the saving comes from not allocating more memory; and all the algorithms would benefit from that.


      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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1214847]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2020-12-01 00:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?