in reply to Re^2: Battle Royal Grep vs For
in thread Battle Royal Grep vs For
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Battle Royal Grep vs For
by Herkum (Parson) on Mar 19, 2008 at 18:01 UTC |