#!/usr/bin/perl use strict; use warnings; use feature 'say'; use List::Util (); use List::Gen qw(:modify :filter); use Benchmark 'cmpthese'; for my $first (0, 1) { my $list = [$first, (1) x (1_000_000)]; say "first: $first"; cmpthese(0, { lu => sub {all_lu($list)}, for => sub {all_for($list)}, lg => sub {all_gen($list)}, }); say ''; }; say "chaining:"; my @list = (1 .. 10_000); # Lookahead (default) would generate one more value local $List::Gen::LOOKAHEAD = 0; cmpthese(0, { mgm => sub { (map {$_->[0]} grep {!($_->[1] % 10)} map {[$_, tf($_)]} @list)[0] }, gfg => sub { (gen {$_->[0]} filter {!($_->[1] % 10)} gen {[$_, tf($_)]} \@list)->[0] }, }); # artificially "heavy" transformation. sub tf { select(undef, undef, undef, 0.000001); $_[0] + 1; } # short-circuit "all" sub all_lu { List::Util::all {$_} @{shift()}; } # short-circuit for-loop sub all_for { for (@{shift()}) { return 0 unless $_; } 1; } # short-circuit lazy generator sub all_gen { !List::Gen(shift)->do(sub {&last(1) unless $_}); } __DATA__ first: 0 Rate lu lg for lu 396/s -- -99% -100% lg 27574/s 6869% -- -100% for 10156692/s 2567064% 36734% -- first: 1 Rate lg for lu lg 4.62/s -- -90% -93% for 48.5/s 949% -- -31% lu 70.6/s 1427% 46% -- chaining: Rate mgm gfg mgm 12.5/s -- -100% gfg 3790/s 30320% --