Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: When does it pay to use the schwartzian transform?

by Roy Johnson (Monsignor)
on Dec 16, 2005 at 19:49 UTC ( [id://517346]=note: print w/replies, xml ) Need Help??


in reply to When does it pay to use the schwartzian transform?

Sorting is an O(n log n) operation, so the comparison sub gets called on average more than once for each element of the list being sorted. So if you have some sort of transformation to do to the data, you're going to be doing it multiple times for at least some of the elements if you include the transformation in the comparison sub.

If the transformation is expensive relative to building an arrayref and dereferencing it, the ST will give you gains in efficiency as the number of elements to be sorted increases, because you do the transformation exactly once per element.

Another way to get the do-it-only-once efficiency would be to Memoize (or cache the results from) the transformation function.


Caution: Contents may have been coded under pressure.
  • Comment on Re: When does it pay to use the schwartzian transform?

Replies are listed 'Best First'.
Re^2: When does it pay to use the schwartzian transform?
by BrowserUk (Patriarch) on Dec 16, 2005 at 20:15 UTC
    Another way to get the do-it-only-once efficiency would be to Memoize (or cache the results from) the transformation function.

    Also known as the Orcish Maneuver, and often more efficient.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

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

    No recent polls found