Impressive how this script was transmitted to us from one year in the future ;-)

In the real code, will you be using subs like your compareAsString, or will you just write sort @whatever? If the latter, then here you're benchmarking code that you've artificially slowed down with the overhead of lots of sub calls.

Also, you might be surprised at the results: the Schwartzian Transform won't give you any improvement for simple comparisons! More specifically, you'll have no performance gain as long as the overhead of the two transforms is greater than the performance gain of the actual sort (makes sense, no?). Throwing in a usleep(1), as you did, will show you that, but you're still not testing the execution time of actual code.

So what the previous two paragraphs are getting at: What is the actual, specific code you'd like to benchmark?

The code itself is a bit problematic: You've declared all of your subs with an empty prototype, but then they all have parameters, which only works because you're using the older &subname calling syntax, which avoids prototypes. And, you're re-using the variable names $a and $b, which are special in sort functions (not necessarily wrong, just confusing). I'd just write these short sort routines inline.

Also, you could be using the Benchmark module to do your benchmarks for you. Here's a quick example (disclaimer: I'm not a Benchmarking expert but I hope I got it right).

#!/usr/bin/env perl use warnings; use strict; use Benchmark 'cmpthese'; use List::Util 'shuffle'; use Data::Dumper; my @sorted; my @input = map { 'F' x (int(rand(100))+1) } 1 .. 1000; #print Dumper(\@input); cmpthese(1000, { simple => sub { @sorted = sort { length($a)<=>length($b) } shuffle @input; }, schwartz => sub { @sorted = map { $$_[0] } sort { $$a[1]<=>$$b[1] } map { [$_,length($_)] } shuffle @input; }, }); #print Dumper(\@sorted);

The results I get are something like this:

Rate schwartz simple schwartz 249/s -- -47% simple 472/s 90% --

I.e. the normal sort is almost twice as fast! However, once you start making the criteria more complex (in the example replace length() with some_complex_computation()), then you'll start seeing the performance boost.


In reply to Re: Benchmark Schwartzian Transform by Anonymous Monk
in thread Benchmark Schwartzian Transform by Anonymous Monk

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.