Skip to content

Instantly share code, notes, and snippets.

@chernomyrdin
Created July 5, 2013 08:24
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 chernomyrdin/5932925 to your computer and use it in GitHub Desktop.
Save chernomyrdin/5932925 to your computer and use it in GitHub Desktop.
Operator if
#!/usr/bin/perl -w --
use strict;
use Benchmark;
my $num = 3;
my $match = 0;
timethese( 1e8, {
just_if => sub {
if ($num == 0 or $num == 1 or $num == 2 or $num == 3 or $num == 4 or $num == 5) {
$match++;
}
},
if_grep => sub {
if (scalar(grep { $_ == $num } (0,1,2,3,4,5))) {
$match++;
}
},
} );
__END__
@chernomyrdin
Copy link
Author

Benchmark: timing 100000000 iterations of if_grep, just_if...
   if_grep: 51 wallclock secs (50.97 usr +  0.18 sys = 51.15 CPU) @ 1955034.21/s (n=100000000)
   just_if: 17 wallclock secs (15.94 usr +  0.08 sys = 16.02 CPU) @ 6242197.25/s (n=100000000)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment