Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Speed comparison of foreach vs grep + map

by ikegami (Patriarch)
on May 25, 2025 at 18:28 UTC ( [id://11165116]=note: print w/replies, xml ) Need Help??


in reply to Speed comparison of foreach vs grep + map

As Corion pointed out, the switch from !$_ to /0/ is overpowering the other differences and invalidating your test.

But other improvements can be made.


There's no reason to use an intermediate array in the latter snippet. This avoids the creation of 5 million scalars.

my @e = grep !$_, map $_ % 2, @c;

You could even eliminate the grep.

my @e = map $_ % 2 ? () : 0, @c;

Similar improvements can be made for the foreach version.

my @e; for my $c ( @c ) { my $d = $c % 2; push @e, $d if !$d; }
my @e; for my $c ( @c ) { push @e, 0 if !( $c % 2 ); }

Finally, I'm curious how these compare:

my @e = ( 0 ) x grep !( $_ % 2 ), @c;
my @e; push @e, 0 for 1 .. grep !( $_ % 2 ), @c;

Replies are listed 'Best First'.
Re^2: Speed comparison of foreach vs grep + map
by ikegami (Patriarch) on May 29, 2025 at 16:48 UTC

    Benchmarks:

    #!/usr/bin/perl use strict; use warnings; use Benchmark qw( cmpthese ); my @c = 1 .. 1_000_000; cmpthese( -3, { for => sub { my @e; for my $c ( @c ) { my $d = $c % 2; push @e, $d if !$d; } }, for2 => sub { my @e; for my $c ( @c ) { push @e, 0 if !( $c % 2 ); } }, map_grep => sub { my @e = grep !$_, map $_ % 2, @c; }, map => sub { my @e = map $_ % 2 ? () : 0, @c; }, grep_x => sub { my @e = ( 0 ) x grep !( $_ % 2 ), @c; }, grep_for => sub { my @e; push @e, 0 for 1 .. grep !( $_ % 2 ), @c; }, } );
    Rate map_grep for grep_for map for2 grep_x map_grep 10.6/s -- -27% -43% -47% -51% -54% for 14.6/s 38% -- -21% -27% -33% -36% grep_for 18.4/s 75% 27% -- -7% -15% -20% map 19.9/s 88% 36% 8% -- -9% -13% for2 21.7/s 106% 49% 18% 9% -- -5% grep_x 22.9/s 117% 57% 24% 15% 6% -- Rate map_grep for grep_for map for2 grep_x map_grep 12.2/s -- -16% -34% -40% -45% -49% for 14.6/s 20% -- -22% -29% -35% -39% grep_for 18.6/s 52% 27% -- -9% -17% -23% map 20.5/s 68% 40% 10% -- -8% -15% for2 22.4/s 83% 53% 20% 9% -- -7% grep_x 24.1/s 97% 65% 29% 17% 8% -- Rate map_grep for grep_for map for2 grep_x map_grep 12.1/s -- -19% -31% -36% -45% -49% for 14.8/s 23% -- -15% -21% -32% -37% grep_for 17.4/s 44% 17% -- -8% -21% -27% map 18.8/s 56% 27% 8% -- -14% -21% for2 21.9/s 82% 48% 26% 16% -- -8% grep_x 23.7/s 97% 60% 37% 26% 8% --

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2025-07-09 00:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.