in reply to Re: Array Sort
in thread Array Sort

It is worth noting that for the complication of the ST to save the OP any time over the standard sort, he would have to be sorting at least a 1 million elements.

And if he is sorting that much data, then he'll need every gain he can get, in which case the GRT suggested by javafan is far more effective. And it is simpler than the ST.

All in all, I wonder if there is ever a situation where the ST makes sense?


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".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^3: Array Sort
by ikegami (Patriarch) on May 10, 2011 at 19:18 UTC

    All in all, I wonder if there is ever a situation where the ST makes sense?

    Over plain sort, sure. Over GRT? Not so much.

    ST can return objects, whereas GRT returns strings unless an external array is used.

    ST can produce simpler and more readable code than GRT in some circumstances.

    However, ST isn't as fast as GRT.

    # ST my @fhs = map $_->[0], sort { $a->[1] <=> $b->[1] } map [ $_, fileno($_) ], get_fhs();
    # GRT my @fhs = get_fhs(); @fhs = map $fhs[ unpack('x4 N', $_) ] sort map pack('NN', fileno($fhs[$_]), $_), 0..$#fhs;
    # GRT my @fhs = get_fhs(); @fhs = @fhs[ map unpack('x4 N', $_), sort map pack('NN', fileno($fhs[$_]), $_), 0..$#fhs ];

      My point was that it requires a pretty expensive key extraction for the ST to be beneficial.

      For example, with fileno the ST never gets a look in and it requires a 1000+ files before the GRT starts to make inroads:

      c:\test>junk92 *.txt Files: 46 Rate ST GRT1 GRT2 sort ST 9206/s -- -21% -27% -58% GRT1 11725/s 27% -- -8% -46% GRT2 12681/s 38% 8% -- -42% sort 21855/s 137% 86% 72% -- c:\test>junk92 *.pl Files: 657 Rate ST GRT1 GRT2 sort ST 411/s -- -39% -44% -46% GRT1 675/s 64% -- -8% -11% GRT2 735/s 79% 9% -- -3% sort 757/s 84% 12% 3% -- c:\test>junk92 * Files: 1293 Rate ST GRT1 sort GRT2 ST 186/s -- -41% -45% -46% GRT1 315/s 70% -- -7% -8% sort 338/s 82% 7% -- -1% GRT2 341/s 84% 8% 1% --

      So, by the time you get to the point where the standard sort, even with numeric keys, can be beaten, you might as well move to straight to a GRT and have done with it.

      sort => q[ my @fhs = sort { fileno( $a ) <=> fileno( $b ) } @fhs; ],

      Others per your code.


      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".
      In the absence of evidence, opinion is indistinguishable from prejudice.