And as usual grep EXPR, LIST that everyone forgets about wins the race.
#!/usr/bin/perl use strict; use warnings; my @values = qw(test test1 test2 test3); use Benchmark qw( cmpthese ); cmpthese -2, { for_loop => sub { my @matches; for (@values) { push @matches, $_ +if $_ =~ /test/ }; }, grep_loop => sub { my @matches = grep { $_ =~ /test/ } @values; + }, grep_expr => sub { my @matches = grep $_ =~ /test/, @values + }, }; __END__ Rate grep_loop for_loop grep_expr grep_loop 248507/s -- -27% -46% for_loop 338839/s 36% -- -27% grep_expr 461521/s 86% 36% --
You owe the oracle a Battle Royale With Cheese and a tasty drink to wash it down with.
The cake is a lie.
The cake is a lie.
The cake is a lie.
In reply to Re^3: Battle Royal Grep vs For
by Fletch
in thread Battle Royal Grep vs For
by Herkum
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |