Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
All:
Calling a sub to do something takes just a little bit longer (microseconds I have been told) than if you copied and pasted the code in the sub and replaced the sub call with it. The difference is very small and for the sake of maintainability - it is worth the hit.

Beware premature optimization - dragons be hiding ahead.

Now I just happen to have be writing a script that reused the same 5 lines of code several times within a loop, so I made it a sub. This particular loop goes through millions of iterations. Now the difference in run times between using a sub versus copying and pasting the code was under 1 minute, but I wondered if there was a way to get the outcome of copying and pasting, but keep the maintainability of a sub.

I think in Perl 6 (I am oblivious), that you will be able to do just this (if the conditions are right). I asked in the CB and I got various suggestions. I was going to let it drop, but bart prompted me to post here if for no other reason than to have a node to Super Search on when someone else asks the same question.

The suggestions I have heard so far:

  • eval
  • Source Filter

    If someone knows how to do this without a lot of work - then please divulge. If there is no easy answer, then there is no need to pursue it. If you need that much speed - Perl probably isn't the right solution in the first place ;-)

    Cheers - L~R


    In reply to Inline subs? by Limbic~Region

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



    • Are you posting in the right place? Check out Where do I post X? to know for sure.
    • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
      <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
    • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
    • Want more info? How to link or How to display code and escape characters are good places to start.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Domain Nodelet?
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others examining the Monastery: (2)
    As of 2024-04-25 05:39 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found