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

I have two set of perl codes. Please let me know which one is best on the parameter such as , memory usage, execution time
FUNC("test",$flag); sub FUNC{ print $_[0] if ($_[1]); }
My second code is
Func("test") if $flag); sub FUNC{ print $_[0];}
I have this kind of code 4-5 times in my script. I am confused here which one to use considering above parameters. Please help. Also it would be nice if somebody can explain how to calculate this parameters. Also, if there is any more parameter to decide which one to use please mention. Thanks

Replies are listed 'Best First'.
Re: How to select the code on basis of memory usage and execution time.
by GrandFather (Saint) on Nov 04, 2008 at 05:54 UTC

    As presented there is the small cost of calling the sub which is avoided in the second case. However if the test is non-trivial it may be better to localise that in the sub to improve maintainability and clarity of the code and wear the small runtime cost of calling the sub. Almost always you will waste far more time worrying about a few microseconds of overhead than will ever be a problem or repay the worry time.

    You may find the replies to Calling a subroutine - which is most efficient? helpful. Bottom line: there's almost no difference so do whatever is easiest to understand and maintain.


    Perl reduces RSI - it saves typing
Re: How to select the code on basis of memory usage and execution time.
by chromatic (Archbishop) on Nov 04, 2008 at 07:44 UTC

    print does IO by way of a system call, which means that you have at least one context switch into kernel mode (to perform the system call) and one context switch out of kernel mode (to return control to the process running perl), and it's likely a blocking system call, so the minor difference between pushing a couple of arguments onto the stack and making a sub call in Perl is mostly immaterial. Besides that, an important question is how often $flag is true, or how often FUNC() gets called, or if this is anywhere near a critical loop in your program (though I have confidence that it's not).

    In other words, don't worry about this code. It's not likely to matter, even if you could measure it without taking into account the system call.

Re: How to select the code on basis of memory usage and execution time.
by CountZero (Bishop) on Nov 04, 2008 at 07:20 UTC
    If you have the same test every time, it is more efficient memory-wise to have the test in the sub-routine, but that will carry a small loss of efficiency in running the program as far as execution time goes because you will call the subroutine and only then test if it was necessary.

    In both cases however, the difference will be unnoticeable for all practical purposes unless you call the subroutine over-and-over again.

    Personally I would say, go with what feels most natural for yourself. If the whole of the subroutine depends on this flag, I would leave the test outside of the subroutine as that indicates more clearly --IMHO-- that the running of the subroutine depends on the value of the flag

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: How to select the code on basis of memory usage and execution time.
by JavaFan (Canon) on Nov 04, 2008 at 11:46 UTC
    If you memory and or execution times are so important for your project that you have to consider these decisions for trivial code like this, your biggest mistake is to use Perl.

    Perl shines in many things of which making it the programmer easy isn't the least of things. But they come with a price. Perl does a lot of bookkeeping behind the scenes. Which costs time and memory.

    Seriously, if memory usuage and/or CPU usage for trivial code matters to you, use C, not Perl. That's were C shines.

      A Java Fan suggests using C on a Perl website?

      I think I agree with the technical recommendation, but I can't help but smile at the conjunction.

      --
      .sig : File not found.