Blob


1 #!/usr/bin/perl
3 # Read up on the Benchmark module, included with Perl. Write a program that
4 # will answer the question, "How much does using the Schwartzian Transform
5 # speed up the task of Exercise 1?"
7 use v5.24;
8 use warnings;
9 use strict;
10 use utf8;
12 use Benchmark qw(:all) ;
14 my $t0, my $t1, my $td;
16 sub naive {
17 chdir; # the default is our home directory;
18 my @sorted = sort { -s $a <=> -s $b } glob '*';
19 }
21 sub schwartz {
22 chdir;
23 my @sorted = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [$_, -s] } glob '*';
24 #print join "\n", @sorted;
25 }
27 $t0 = Benchmark->new;
28 timethis(1000, \&naive);
29 $t1 = Benchmark->new;
30 $td = timediff($t1, $t0);
31 print "the code took:",timestr($td),"\n";
33 $t0 = Benchmark->new;
34 timethis(1000, \&schwartz);
35 $t1 = Benchmark->new;
36 $t1 = Benchmark->new;
37 $td = timediff($t1, $t0);
38 print "the code took:",timestr($td),"\n";