Created
June 20, 2011 08:56
-
-
Save yko/1035329 to your computer and use it in GitHub Desktop.
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 | |
use Benchmark qw(:all); | |
sub with_return { | |
my $x = shift; | |
return $x; | |
} | |
sub without_return { | |
my $x = shift; | |
$x; | |
} | |
cmpthese( | |
10_000_000, | |
{ 'with return' => \&with_return, | |
'without return' => \&without_return, | |
} | |
); | |
__END__ | |
$ perl ret.t # 5.14.0 | |
Rate with return without return | |
with return 7042254/s -- -8% | |
without return 7692308/s 9% -- | |
$ /usr/bin/perl ret.t # 5.10.1 | |
Rate with return without return | |
with return 4807692/s -- -26% | |
without return 6535948/s 36% -- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I must warn you that this measuring technique is far from being perfect and results are not absolutely truth.
For example, running this test on perl 5.10.1
This is perl, v5.10.1 (*) built for i686-linux-gnu-thread-multi
(with 53 registered patches, see perl -V for more detail)
gives different results. 'with return' can be 3 ..28% with median tends to be 20%. Obvious my first run was not real case and 36% was too big difference to be real.
The same thing on perl 5.14.0, but median tends to be closer to 8-9%
This gist does not try to compare return speed on different perl versions. It just shows that calling return actually costs.
For sure, you should not care about it every time you write something. But when you are going to optimize code that serves tons of calls it can be useful.