Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Efficiency and overhead of subs

by chromatic (Archbishop)
on Nov 16, 2001 at 09:08 UTC ( #125765=note: print w/ replies, xml ) Need Help??


in reply to Efficiency and overhead of subs

There is overhead, and my understanding is that much of it is the stuff that happens when entering and leaving blocks. Subs count. A chunk of that is spent figuring out the correct lexical scope to use.

You can get a decent idea with a benchmark like the following:

use Benchmark; sub asub { for (1 .. 2000) {} } Benchmark::cmpthese (5000, { 'sub' => sub { asub() }, 'nosub' => sub { for (1 .. 2000) {} }, });
With 5.6.1 on my Linux box, there's a 4% penalty for subroutines. With the latest development version, it's so close to 0% as to be statistically insignificant.

I doubt you'll find many cases where using subroutines effectively outweighs the performance hit of loading a Perl interpreter and compiling your program in a CGI environment each time. Besides that, if you unroll subs enough, you'll probably ruin locality of reference.

Perl's more about developer ease than efficiency/small memory footprint anyway. Sometimes that matters.


Comment on Re: Efficiency and overhead of subs
Download Code
Re: Re: Efficiency and overhead of subs
by Lexicon (Chaplain) on Nov 18, 2001 at 05:05 UTC
    As usual, it's already been done before as well. I asked this same question basically about 8 months ago I suppose. The discussion is over here at Fundamental Benchmarks. Hope this helps out!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://125765]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2014-12-29 11:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (186 votes), past polls