Skip to content

Instantly share code, notes, and snippets.

@dctabuyz
Forked from cynovg/bench.pl
Last active January 10, 2019 18:08
Show Gist options
  • Save dctabuyz/c70b741d83ca8beb116ce7bc67392853 to your computer and use it in GitHub Desktop.
Save dctabuyz/c70b741d83ca8beb116ce7bc67392853 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use v5.26.0;
use List::Util qw/first any/;
use Benchmark qw(:all);
for my $m ( 1 .. 3 ) {
my $max = $m * 1000;
my @data = map { "x$_" } ( 1 .. $max );
timethese(100_000, {
"foreach x $m" => sub { foreach ( @data ) { return 1 if $_ eq $max; } return 0; },
"any x $m" => sub { return 1 if any {$_ eq $max} ( @data ); return 0; },
"grep x $m" => sub { return 1 if scalar grep {$_ eq $max} ( @data ); return 0; },
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment