Anonymous subs are cheaper in terms of usage (invoking a named sub includes a round-trip to the symbol table) and memory.

Named subs:

use strict; use warnings; my $begin; BEGIN { chop( $begin = `ps -o vsz= $$` )} eval "sub F_$_ { my( \$a, \$b, \$c ) = \@_; my \$x = $_; return \$a * \$b - (\$x + \$c); }" for 1..1e4; END { chop( my $end = `ps -o vsz= $$` ); print "$end - $begin = ",$end - $begin, "\n"; } __END__ 60924 - 22228 = 38696

Anonymous subs:

use strict; use warnings; my $begin; BEGIN { chop( $begin = `ps -o vsz= $$` )} my @ary; $ary[$_] = eval "sub { my( \$a, \$b, \$c ) = \@_; my \$x = $_; return \$a * \$b - (\$x + \$c); }" for 1..1e4; END { chop( my $end = `ps -o vsz= $$` ); print "$end - $begin = ",$end - $begin, "\n"; } __END__ 53356 - 22228 = 31128

That makes ca. 3.1kB per anonsub, 3.9 per named sub. Slightly different numbers than BrowserUk's above, but also slightly different architecture and version of perl:

perl 5, version 14, subversion 2 (v5.14.2) built for x86_64-linux-gnu-thread-multi

update: The ca. 750 extra bytes for named subs may well be just the cost of allocating a GLOB for the subroutine in the symbol table, which comes wiith SCALAR, HASH, ARRAY, CODE and FILEHANDLE slots. Currently I don't recall whether they are autovivified as needed or allocated in one go.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

In reply to Re: Memory efficiency, anonymous vs named's vs local subroutines by shmem
in thread Memory efficiency, anonymous vs named's vs local subroutines by thanos1983

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.