Hopefully, this is an interesting question about the Schwartzian Transform and Perl internal performance:

I thought I was going to demonstrate the performance advantage of the Schwartzian Transform by applying it to the three-way sort code posted by BBQ. It is slower, however, with my quick implementation (which looks right...)

#!/usr/bin/perl -w use Benchmark; timethese(100, { a => q{ my %hash = (key1 => 3, key2 => 2, key3 => "test"); # I used 10000 more for a large test set (omitted for +brevity) @keys = sort { $hash{$b} cmp $hash{$a} || length($b) cmp length($a) || $b <=> $a } keys %hash }, b => q{ my %hash = (key1 => 3, key2 => 2, key3 => "test"); # I used the same 10000 more as above @keys = map { $_->[0] } sort { $b->[1] cmp $a->[1] || $b->[2] <=> $a->[2] || $b->[0] cmp $a->[0] } map{ [$_, $hash{$_}, length $_] } keys %hash; } });
Output:
Benchmark: timing 100 iterations of a, b... a: 274 wallclock secs (272.26 usr + 0.22 sys = 272.48 CPU) b: 366 wallclock secs (359.06 usr + 0.52 sys = 359.58 CPU)
So, now it's once again my turn to learn (happens all the time around here -- I love Perl Monks)...

The Schwartzian Transform is all about reducing the computational difficulty of comparison code during complicated sorts. In this case, I assumed that two hash lookups, followed by two string length calculations, finally followed by a plain comparison would easily qualify as a "complicated" sort block.

What's happening, here? Why does it take longer to dereference and index into an array than to do hash lookups and length() calls?

Russ

P.S. My omitted data had all of the values equal (to force the comparison to always do the most work). It is just generated with map(($_, 1), (1..10000)).


In reply to Schwartzian Transform vs. plain Perl by Russ

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.