Created
May 7, 2022 23:35
-
-
Save azumakuniyuki/f43217e18bf93a913eff4700fa577078 to your computer and use it in GitHub Desktop.
How is faster the Lexical Subroutine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# my $p = sub {} vs. my sub p {} | |
use strict; | |
use warnings; | |
use Benchmark ':all'; | |
use Test::More 'no_plan'; | |
sub sr1 { | |
my $p = sub { return 2 * shift }; | |
return $p->(shift); | |
} | |
sub sr2 { | |
my sub p { return 2 * shift }; | |
return p(shift); | |
} | |
is sr1(2), 4; | |
is sr2(2), 4; | |
printf("Running with Perl %s on %s\n%s\n", $^V, $^O, '-' x 80); | |
cmpthese(6e6, { | |
'my $p = sub {}' => sub { sr1(2) }, | |
'my sub p {...}' => sub { sr2(2) }, | |
} | |
); | |
__END__ | |
Running with Perl v5.30.0 on darwin | |
-------------------------------------------------------------------------------- | |
Rate my sub p {...} my $p = sub {} | |
my sub p {...} 1197605/s -- -76% | |
my $p = sub {} 4958678/s 314% -- | |
Running with Perl v5.32.0 on darwin | |
-------------------------------------------------------------------------------- | |
Rate my sub p {...} my $p = sub {} | |
my sub p {...} 1315789/s -- -73% | |
my $p = sub {} 4878049/s 271% -- | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment