Skip to content

Instantly share code, notes, and snippets.

@cynovg
Created January 10, 2019 16:54
Show Gist options
  • Save cynovg/87dbb806051ba52894835c295e02e3b1 to your computer and use it in GitHub Desktop.
Save cynovg/87dbb806051ba52894835c295e02e3b1 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);
timethese(10000, {
'foreach' => sub {
foreach (1..1000) {
last if $_ == 1000;
}
return;
},
'any' => sub {
return if any {$_ == 1000} (1..1000);
},
});
@cynovg
Copy link
Author

cynovg commented Jan 10, 2019

Benchmark: timing 10000 iterations of any, foreach...
       any:  0 wallclock secs ( 0.31 usr +  0.00 sys =  0.31 CPU) @ 32258.06/s (n=10000)
            (warning: too few iterations for a reliable count)
   foreach:  1 wallclock secs ( 0.28 usr +  0.00 sys =  0.28 CPU) @ 35714.29/s (n=10000)
            (warning: too few iterations for a reliable count)

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