Third improvement, which returned poorer results, for Schwartzian but posting it, maybe someone will spot an error. :-)
#! /usr/bin/perl -w ############################################################# # File : Perl Schwartzian Transform Demo.pl # History: 16-Mar-2015 Laszlo first version. # 19-Mar-2015 Laszlo, improved a little bit. # 19-Mar-2015 Laszlo, did the benchmarking which returned p +oorer results... ############################################################# # A script to demonstrate and measure the Schwartzian Transform. ############################################################# # Some form of documentation: # http://www.perlmonks.org/?node_id=1120222 use warnings; use strict; use Time::HiRes; use Benchmark; use List::Util; my $maxNumberValue = 100; my $intSampleSize = 32000; my @unsortedIntArray = map { int(rand($maxNumberValue)) } ( 1..$intSam +pleSize ); my $compareAsNumberFunction = sub { return $_[0] <=> $_[1]; }; my $compareAsStringFunction = sub { return $_[0] cmp $_[1]; }; my $noTransformFunction = sub { return shift; }; my $sleepyNoTransformFunction = sub { Time::HiRes::usleep(1); return s +hift; }; my $returnLengthFunction = sub { return length(shift); }; schwartzianSortVsRegularSort(\@unsortedIntArray, $compareAsNumberFunct +ion, $noTransformFunction); schwartzianSortVsRegularSort(\@unsortedIntArray, $compareAsStringFunct +ion, $noTransformFunction); schwartzianSortVsRegularSort(\@unsortedIntArray, $compareAsNumberFunct +ion, $sleepyNoTransformFunction); my @chars = ("A".."Z", "a".."z"); my $maxStringLength = 300; my $stringSampleSize = 32000; my @unsortedStringArray = map { (sub { my $string; $string .= $chars[rand @chars] for 1..(int(rand($maxSt +ringLength)) + 1); return $string; })->(); } ( 1..$stringSampleSize ); schwartzianSortVsRegularSort(\@unsortedStringArray, $compareAsStringFu +nction, $noTransformFunction); schwartzianSortVsRegularSort(\@unsortedStringArray, , $compareAsNumber +Function, $returnLengthFunction); sub schwartzianSortVsRegularSort { my @unsortedArray_in = @{$_[0]}; my $compareFunction_in = $_[1]; my $transform4Sort_in = $_[2]; print "Collection of size: " . +@unsortedArray_in . " \n"; Benchmark::cmpthese(10, { regular => sub { my @regularSorted = sort { $compareFunction_in->($transfor +m4Sort_in->($a), $transform4Sort_in->($b)) } List::Util::shuffle @uns +ortedArray_in; }, schwartzian => sub { my @schwartzianSorted = map { $_->[0] } sort { $compareFunction_in->($a->[1], $b->[1]) } map { [$_, $transform4Sort_in->($_)] } List::Util::shuffle @unsortedArray_in; } }); }

In reply to Re: Benchmark Schwartzian Transform by Laszlo
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.