Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: printing every 2nd entry in a list backwards

by tobyink (Abbot)
on May 19, 2017 at 15:47 UTC ( #1190641=note: print w/replies, xml ) Need Help??


in reply to printing every 2nd entry in a list backwards

use v5.16; use List::Util qw/pairmap/; # this is in core if you have a recent Per +l my @list = qw( 1 22 3 -4 5 666 7 ); say pairmap { "$b " } reverse @list;

Assuming you have the XS version of List::Util installed, this should blow away the grep/map solutions in terms of speed.

use v5.16; use List::Util qw/pairmap/; use Test::Modern qw( is_deeply is_fastest done_testing ); our @list = ( qw( 1 22 3 -4 5 666 7 ), 8 .. 99_999, ); my %implementations = ( pairmap => sub { no warnings; my @x = pairmap { $a } reverse @:: +list }, grepstate => sub { my @x = grep { state $i; $i++ % 2 +== 0 } reverse @::list }, grepmy => sub { my $i; my @x = grep +($i++ % 2 == 0), rev +erse @::list }, grepbit => sub { my $i; my @x = grep $i^=1, reverse @::lis +t }, mapmy => sub { my $i; my @x = map +($i++ & 1 ? () : $_), + reverse @::list }, mapidx => sub { my @x = map $::list[$#::list-$_*2] +, 0..$#::list/2 }, ); # Known good result my $reference = [ $implementations{pairmap}->() ]; for my $key (sort keys %implementations) { is_deeply( [ $implementations{$key}->() ], $reference, "$key produces correct output", ); } { local $Test::Modern::VERBOSE = 1; is_fastest('pairmap', -3, \%implementations); } done_testing; __END__ ok 1 - grepbit produces correct output ok 2 - grepmy produces correct output ok 3 - grepstate produces correct output ok 4 - mapidx produces correct output ok 5 - mapmy produces correct output ok 6 - pairmap produces correct output ok 7 - pairmap is fastest # pairmap - 3 wallclock secs ( 3.24 usr + 0.01 sys = 3.25 CPU) @ 17 +5.08/s (n=569) # grepbit - 3 wallclock secs ( 3.23 usr + 0.00 sys = 3.23 CPU) @ 10 +9.91/s (n=355) # mapidx - 3 wallclock secs ( 3.18 usr + 0.00 sys = 3.18 CPU) @ 77. +36/s (n=246) # mapmy - 3 wallclock secs ( 3.18 usr + 0.01 sys = 3.19 CPU) @ 76.1 +8/s (n=243) # grepmy - 3 wallclock secs ( 3.24 usr + 0.01 sys = 3.25 CPU) @ 68. +62/s (n=223) # grepstate - 3 wallclock secs ( 3.12 usr + 0.00 sys = 3.12 CPU) @ +33.33/s (n=104) ok 8 - no (unexpected) warnings (via done_testing) 1..8

UPDATE: Added the grep $i^=1, reverse solution, which is fast, but still loses to pairmap.

Replies are listed 'Best First'.
Re^2: printing every 2nd entry in a list backwards
by shmem (Chancellor) on May 19, 2017 at 18:19 UTC

    Here's a mapslice

    mapslice => sub { my @x = @::list[ map {$_<<1} reverse 0..@::list>>1 ] + }

    which you might want to add.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      Yeah, that's pretty fast.

      # pairmap - 4 wallclock secs ( 3.18 usr + 0.00 sys = 3.18 CPU) @ 17 +5.47/s (n=558) # grepbit - 3 wallclock secs ( 3.26 usr + 0.00 sys = 3.26 CPU) @ 11 +4.72/s (n=374) # mapslice - 3 wallclock secs ( 3.25 usr + 0.00 sys = 3.25 CPU) @ 1 +08.92/s (n=354) # mapidx - 3 wallclock secs ( 3.20 usr + 0.01 sys = 3.21 CPU) @ 76. +64/s (n=246) # mapmy - 3 wallclock secs ( 3.06 usr + 0.00 sys = 3.06 CPU) @ 73.8 +6/s (n=226) # grepmy - 3 wallclock secs ( 3.01 usr + 0.00 sys = 3.01 CPU) @ 69. +44/s (n=209) # grepstate - 3 wallclock secs ( 3.17 usr + 0.00 sys = 3.17 CPU) @ +34.07/s (n=108)
Re^2: printing every 2nd entry in a list backwards
by 1nickt (Prior) on May 19, 2017 at 16:31 UTC

    tobyink: is_fastest, cool! You always have the newest toys! Do you have an opinion about why grep with state is so much slower than grep with my?


    The way forward always starts with a minimal test.

      Well, it necessitates using a block, and the block form of grep is a little slower than the expression form.

      grep { BLOCK } @list; # slow grep EXPRESSION, @list; # fast

      But it's not just that. Even if you make them both use the block form of grep, state comes out behind my. I guess they've just put a lot of work into optimizing how fast normal lexical variables work, and less work into state variables.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1190641]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2017-08-19 12:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (310 votes). Check out past polls.

    Notices?