Okie, I learned a little bit from critique and here are my first improvements. (Created account name too).
#! /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. ###################################################################### +## # 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; 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[r +and @chars] for 1..(int(rand($maxStringLength)) + 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"; # Schwartzian sort, Schwartzian sort, Schwartzian sort, Schwartzia +n sort, Schwartzian sort ################################################################## +######################## my $schwartzianStart = Time::HiRes::time; my @schwartzianSorted = map { $_->[0] } sort { $compareFunction_in->($a->[1], $b->[1]) } map { [$_, $transform4Sort_in->($_)] } @unsortedArray_in; my $schwartzianDuration = Time::HiRes::time - $schwartzianStart; # Regular sort, Regular sort, Regular sort, Regular sort, Regular +sort, Regular sort ################################################################## +######################## my $regularStart = Time::HiRes::time; my @regularSorted = sort { $compareFunction_in->($transform4Sort_i +n->($a), $transform4Sort_in->($b)) } @unsortedArray_in; my $regularDuration = Time::HiRes::time - $regularStart; print "Execution time of the Schwartzian Transform: $schwartzianDu +ration s\n"; print "Execution time of the Regular Transform: $regularDurati +on s\n"; if (!(@schwartzianSorted ~~ @regularSorted)) { die "Collections are not equal!" } #print join("; ", @schwartzianSorted); }
Next improvements will be with the benchmark. :-) Now that gave a worst result...

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.