Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone know what the overhead is on calling subroutines in a perl script with respect to time etc. I have a form that uses many text and pop-up boxes and had made it easy on myself using a subroutine like this
popup_menu(-name=>$_[0],-values=>[@options],-default=>$form->param($_[ +0])); ...etc.
but there are loads of calls to this, so would it be quicker doing the full code in the form or shouldn't it matter that I'm calling the subroutine 20 times? Thanks

Replies are listed 'Best First'.
Re: Speed/resources
by Crulx (Monk) on Feb 29, 2000 at 14:57 UTC
    No matter what programming language you use, inline functions are faster than subroutine calls. However, perl suffers no more overhead for subroutine calls than any other language. Breaking down an easy to read and update perl program just to squeeze a little speed out of it is the wrong way to do it. This is basically true of any language. The only time you want to sacrifice readablity and maintainability is when you have some computationally heavy code that you know will need tweeking. 20 function calles doesn't even come close.
    That being said, your use of @options in the subroutine call poses a bit of a problem. Perl will copy this array, which, depending on its size, could incur some serious overhead. Pass the array by reference.
    ---
    Crulx
    crulx@iaxs.net
      Hmm, isn't writing: [ @options ] going to create an anonymous array containing @options? I think it would be more clear to write: values => \@options however.