http://www.perlmonks.org?node_id=971412


in reply to Re^2: Date to be sorted in descending and time in ascending
in thread Date to be sorted in descending and time in ascending

aaron_baugher:

Hmmm, I'm surprised. I figured at a million strings that the Schwartzian Transform would win. But you show another good lesson: Measure, don't guess. While both you and I expected the transform to win at a million strings, measurement trumps expectation.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

  • Comment on Re^3: Date to be sorted in descending and time in ascending

Replies are listed 'Best First'.
Re^4: Date to be sorted in descending and time in ascending
by aaron_baugher (Curate) on May 19, 2012 at 18:11 UTC

    Yep. For further curiosity, I did a count on how many substr calls there were. For the ST, of course, you have two for each element, one to get the first 8 chars and one to get the rest. So for a million-element array, that's 2M calls. But for the sort-on-substr version, I got about 37M substr calls. That'll vary some depending on how unsorted the original array is, but that's probably a good ballpark number.

    That's a lot more substr calls, but I guess it's still less work than building an entire new million-element array (with each element a reference to a two-element array), as the ST requires.

    It does give me a (very rough) rule of thumb, though: for the ST to be more efficient, the alternative probably needs to do the equivalent of 8-10 substr calls. So just a few core functions probably won't qualify, but a longer series of functions, or some fairly complex regexes, or certainly any sort of file or database lookups, probably will. And there's always measurement to see for sure.

    Aaron B.
    Available for small or large Perl jobs; see my home node.