Skip to content

Instantly share code, notes, and snippets.

@mpapec

mpapec/grepBench Secret

Created May 30, 2016 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpapec/837f9457b61738430fc74c34b93d6732 to your computer and use it in GitHub Desktop.
Save mpapec/837f9457b61738430fc74c34b93d6732 to your computer and use it in GitHub Desktop.
perl -Mstrict -wE'
use Benchmark qw(:all);
my $arr1 = [ 1 .. 10_000 ];
my $arr2 = [ 1 .. 10_000 ];
my $arr3 = [ 1 .. 10_000 ];
cmpthese(-3, {
inPlace => sub {
@$arr1 = grep { $_ ne "100" } @$arr1;
},
newArray => sub {
$arr2 = [ grep { $_ ne "100" } @$arr2 ];
},
newArray2 => sub {
$arr3 = sub {\@_}->(grep { $_ ne "100" } @$arr3);
}
});
'
#
#
Rate inPlace newArray newArray2
inPlace 377/s -- -22% -72%
newArray 486/s 29% -- -64%
newArray2 1334/s 254% 174% --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment