Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Benchmark Schwartzian Transform

by Laszlo (Novice)
on Mar 19, 2015 at 13:30 UTC ( [id://1120578]=note: print w/replies, xml ) Need Help??


in reply to Benchmark Schwartzian Transform

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; } }); }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1120578]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2025-11-08 17:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (65 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.