is there a way I can optimise this subroutine?

You'll need to describe what your subroutine does a whole lot more clearly before anyone will be able to offer any major algorithmic efficiency changes.

For example: "Using the 2 store variables to specify a unique location, I want to get the unique products to write the whole array for that record_entry to an outfile and count the number of unique, duplicated and total products"; how does having "a unique location" allow you to "get the unique products"?

In the mean time, there are some very obvious implementation efficiencies that you could make.

  1. You pass in a hash reference, but then duplicate the entire hash it refers to:
    my %comboHashRef = %{$_[2]};

    You should avoid doing that by accessing the referenced hash indirectly. Ie. Instead of:

    my %comboHashRef = %{$_[2]}; ... foreach my $key (keys %comboHashRef){

    Do:

    my $comboHashRef = $_[2]; ... foreach my $key (keys %{ $comboHashRef } ){

    Or even better, avoid the generation of a large list and do:

    my $comboHashRef = $_[2]; ... while( my( $key, $value ) = each %{ $comboHashRef } ){
  2. Ditto, once you've built up %precords;, you return it thus:
    return %precords;

    And what that does is:

    1. Takes the hash you've so carefully constructed.
    2. flattens it to a big list on the stack;
    3. then garbage collects the entire hash;
    4. returns to the caller, where the big list on the stack is then reconstructed back to a new hash;
    5. And then the big list you constructed on the stack has to be garbage collected.

    Whereas, if your simply returned a reference to the hash you built internally, you avoid all that deconstruction and reconstruction.

    By way of example of the difference this can make, here is a deceptively simple benchmark:

    sub yourWay{ my %h = %{ $_[0] }; return %h; };; sub myWay{ my $ref = shift; return $ref };; %hash = ( 1 .. 1e6 );; cmpthese -5,{ a=>q[ my %h = yourWay( \%hash ); ], b=>q[ my $h = myWay( \%hash ); ] };; s/iter a b a 1.12 -- -100% b 1.03e-006 108368213% --

    And yes, those two subroutines are functionally equivalent. And yes; that figure of 1 million times more efficient is real!

No, those changes won't speed up your subroutine a million times; but they are changes worth making.

Now, can you clarify your description of the processing your sub does with a concrete example or two? If you can, you might get some good responses that will make a real dent in your problem.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.
I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!

In reply to Re: Optimise Sub, make hash unique based on values in array by BrowserUk
in thread Optimise Sub, make hash unique based on values in array by anomilie

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.