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

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Looks like you're right - plain sort beats the heck out of ST in this case (and would do the same to GRT, I'm sure) - length is just too trivial.

Although as the num-* cases below show, it turns out that length is more expensive for data that isn't pre-stringified, at least on the perl 5.14.2 I tested.

#!/usr/bin/perl use strict; use warnings; use Benchmark qw(:all :hireswallclock); foreach my $arraySize(10,100,1_000,10_000,100_000) { my @numdata=map rand, 0..$arraySize; my @strdata=map {sprintf "%d",$_} @numdata; print "========== ARRAY SIZE: $arraySize\n"; cmpthese( -1, { 'str-ST' => sub { my @arr = @strdata; @arr = map { $_->[0] } sort {$a->[1]<=>$b->[1]} map { [$_, length($_)]} @arr; }, 'str-sort' => sub { my @arr = @strdata; @arr = sort {length($a) <=> length($b)} @arr; }, 'num-ST' => sub { my @arr = @numdata; @arr = map { $_->[0] } sort {$a->[1]<=>$b->[1]} map { [$_, length($_)]} @arr; }, 'num-sort' => sub { my @arr = @numdata; @arr = sort {length($a) <=> length($b)} @arr; }, } ); }
Results:
$ perl benchmark-sort.pl
========== ARRAY SIZE: 10
            Rate   num-ST num-sort   str-ST str-sort
num-ST    5119/s       --      -9%     -77%     -90%
num-sort  5598/s       9%       --     -75%     -89%
str-ST   22719/s     344%     306%       --     -55%
str-sort 50539/s     887%     803%     122%       --
========== ARRAY SIZE: 100
           Rate   num-ST num-sort   str-ST str-sort
num-ST    534/s       --     -13%     -82%     -92%
num-sort  615/s      15%       --     -79%     -91%
str-ST   2904/s     444%     372%       --     -58%
str-sort 6854/s    1184%    1014%     136%       --
========== ARRAY SIZE: 1000
           Rate   num-ST num-sort   str-ST str-sort
num-ST   51.8/s       --     -16%     -82%     -93%
num-sort 61.4/s      18%       --     -79%     -92%
str-ST    290/s     460%     373%       --     -61%
str-sort  741/s    1330%    1107%     155%       --
========== ARRAY SIZE: 10000
           Rate   num-ST num-sort   str-ST str-sort
num-ST   5.13/s       --     -16%     -82%     -93%
num-sort 6.15/s      20%       --     -78%     -92%
str-ST   28.2/s     449%     358%       --     -62%
str-sort 73.7/s    1336%    1099%     162%       --
========== ARRAY SIZE: 100000
            (warning: too few iterations for a reliable count)
            (warning: too few iterations for a reliable count)
            (warning: too few iterations for a reliable count)
         s/iter   num-ST num-sort   str-ST str-sort
num-ST     1.93       --     -13%     -81%     -93%
num-sort   1.68      15%       --     -78%     -92%
str-ST    0.364     432%     363%       --     -61%
str-sort  0.142    1258%    1083%     155%       --

Mike

In reply to Re: length() and the Schwartzian transform by RMGir
in thread length() and the Schwartzian transform by hazylife

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!
  • 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, 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, 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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (10)
    As of 2015-07-05 19:28 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (67 votes), past polls