Hi, Guys I was trying to Benchmark Schwartzian Transform versus regular sorting, and wrote a little program, maybe you can improve it. :-)
#! /usr/bin/perl -w ############################################################ # File : Perl Schwartzian Transform Demo.pl # History: 16-Mar-2016 Laszlo first version. ############################################################ # A script to demonstrate and measure the Schwartzian Transform. ############################################################ use warnings; use strict; use Time::HiRes; my $maxNumberValue = 100; my $intSampleSize = 32000; my @unsortedIntArray = map { int(rand($maxNumberValue)) } ( 1..$intSam +pleSize ); &schwartzianSortVsRegularSort(\@unsortedIntArray, \&compareAsNumber, \ +&noTransform); &schwartzianSortVsRegularSort(\@unsortedIntArray, \&compareAsString, \ +&noTransform); &schwartzianSortVsRegularSort(\@unsortedIntArray, \&compareAsNumber, \ +&sleepyNoTransform); 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, \&compareAsString +, \&noTransform); &schwartzianSortVsRegularSort(\@unsortedStringArray, , \&compareAsNumb +er, \&returnLength ); 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); } sub noTransform() { my $param = shift; return $param; } sub sleepyNoTransform() { my $param = shift; Time::HiRes::usleep(1); return $param; } sub returnLength() { my $param = shift; return length($param); } sub sortAsString() { my $param = shift; return "$param"; } sub compareAsNumber() { my $a = shift; my $b = shift; return $a <=> $b; } sub compareAsString() { my $a = shift; my $b = shift; return $a cmp $b; }

In reply to 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.