Skip to content

Instantly share code, notes, and snippets.

@yko
Created June 20, 2011 08:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yko/1035329 to your computer and use it in GitHub Desktop.
Save yko/1035329 to your computer and use it in GitHub Desktop.
#!/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% --
@heikkil
Copy link

heikkil commented Jun 22, 2011

5.10.1., at least with latest these patches, does not do that bad:

bala ~/Documents/perl> perl implicit_return.pl
Rate without return with return
without return 9345794/s -- -7%
with return 10101010/s 8% --
bala ~/Documents/perl> perl -v

This is perl, v5.10.1 (*) built for x86_64-linux-gnu-thread-multi
(with 53 registered patches, see perl -V for more detail)

@yko
Copy link
Author

yko commented Jun 22, 2011

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.

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