Skip to content

Instantly share code, notes, and snippets.

@cynovg
Last active January 24, 2022 11:53
Show Gist options
  • Save cynovg/d429886345d7d24c9bd437a8fe351daf to your computer and use it in GitHub Desktop.
Save cynovg/d429886345d7d24c9bd437a8fe351daf to your computer and use it in GitHub Desktop.
closure
#!/usr/bin/perl
use warnings;
use v5.16;
use Benchmark qw(:all);
timethese(
10000000,
{
cycle => sub {
# test 2
my @best;
push @best, $_ for 1 .. 10;
return @best;
},
closure => sub {
# test 1
my $test = sub {
my @best;
push @best, $_ for 1 .. 10;
return @best;
};
return $test->();
},
real => sub {
my @best;
my $test = sub {
push @best, $_ for 1 .. 10;
};
$test->();
return @best;
},
}
);
@cynovg
Copy link
Author

cynovg commented Apr 11, 2019

cynovg@ubuntu-desktop:~/myshop$ perl bin/test.pl 
Benchmark: timing 10000000 iterations of closure, cycle, real...
   closure:  6 wallclock secs ( 6.35 usr +  0.01 sys =  6.36 CPU) @ 1572327.04/s (n=10000000)
     cycle:  5 wallclock secs ( 5.11 usr +  0.00 sys =  5.11 CPU) @ 1956947.16/s (n=10000000)
      real: 11 wallclock secs (11.45 usr +  0.00 sys = 11.45 CPU) @ 873362.45/s (n=10000000)

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