As shmem says, subroutine/method calls are pretty expensive. You can shave some off by removing the shift, as diotalevi suggests. Removing the return shaves off some more:
use Benchmark qw(:all); my $a = 10; my $b = 20; cmpthese(-2, { 'Sub' => sub { my $c = add ($a, $b); }, 'Method' => sub { my $c = __PACKAGE__->meth_add ($a, $b); }, 'Sub^2' => sub { my $c = add2 ($a, $b); }, 'Method^2' => sub { my $c = __PACKAGE__->meth2 ($a, $b); }, 'Method^3' => sub { my $c = __PACKAGE__->meth3 ($a, $b); }, }); sub add { return $_[0] + $_[1]; } sub add2 { $_[0] + $_[1] } sub meth_add { shift; return $_[0] + $_[1]; } sub meth2 { shift; $_[0] + $_[1] } sub meth3 { $_[1] + $_[2] }
Rate Method Method^2 Method^3 Sub Sub^2 Method 892013/s -- -5% -12% -23% -30% Method^2 936229/s 5% -- -7% -20% -27% Method^3 1008242/s 13% 8% -- -13% -21% Sub 1165141/s 31% 24% 16% -- -9% Sub^2 1275428/s 43% 36% 27% 9% --

In reply to Re: Performance issues with subroutines/methods by ysth
in thread Performance issues with subroutines/methods by ribasushi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.