#!/usr/bin/env perl # ex: set tabstop=4 noexpandtab: use v5.14; use warnings; use Benchmark qw/timethis/; my $count = shift || 10_000; sub grep_in_array { my ($element, @array) = @_; grep {$element eq $_} @array and return 1; return 0; } sub is_in_array { my ($element, @array) = @_; given ("_$element") { when ([map {"_$_"} @array]) { return 1; } } return 0; } my @array = (0..10_000, 'abcd'); for my $element (qw/a ab abc 0 1 10 100 1000 10000 10001 abcd/) { say "Test with: $element"; say "With grep:"; say sprintf("Element %s %s in array", $element, grep_in_array($element, @array) ? "is" : "is not"); say "With given-when:"; say sprintf("Element %s %s in array", $element, is_in_array($element, @array) ? "is" : "is not"); say "With grep:"; timethis($count, sub { grep_in_array($element, @array); }); say "With given-when:"; timethis($count, sub { is_in_array($element, @array); }); }