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