r/perl•
subogero

Perl vs Clojure - Reloaded

After lots of feedback from Clojurists on my rather noob lisp, here is the new version of the performance comparison. New test: shuffle and sort the list of files 1000 times.

The Clojure

(use 'clojure.java.io)
(defn huzza [x] (str "Tiplizz inet " x "!"))
(defn huzzatok [xs] (mapv huzza xs))
(defn ls [dir] (seq (.list (file dir))))
(defn shufsort [n xs]                    
  (if (zero? n)                          
    xs                                   
    (recur (dec n) (sort (shuffle xs)))))
(def files (time (ls "/usr/bin")))
(def sorted (time (huzzatok (sort files))))
(def sortedk (time (huzzatok (shufsort 1000 files))))
(time (run! println sortedk))

The Perl

#!/usr/bin/perl -l
use List::Util qw(shuffle);
use Time::HiRes qw(time);
sub timed {
    my $t = time;
    my @r = shift()->(@_);
    print "\"Elapsed time: ", (time - $t) * 1000, " msecs\"";
    @r;
}
sub huzza { "Tiplizz inet $_[0]!" }
sub huzzatok { map { huzza $_ } @_ }
sub ls { opendir DIR, shift; readdir DIR }
sub shufsort { my ($n, @l) = @_; @l = sort(shuffle(@l)) for 1 .. $n; @l; }

my @files = timed(sub { ls "/usr/bin" });
my @sorted = timed(sub { huzzatok sort @files });
my @sortedk = timed(sub { huzzatok shufsort 1000, @files });
timed(sub { print for @sortedk });

The results [ms]

ClojurePerl
read dir1029
sort str5214
shuffle sort 1000x55887735
print4052

Observations

Clojure wins, at last! But only the long CPU-intensive stuff. By further increasing the number of shuffles and sorts, Clojure becomes twice as fast as Perl.

Perl still rules IO. STDOUT was redirected into a pipe this time, which Perl apparently noticed, and probably switched to page-sized output buffering, resulting in a shocking 2ms for a few thousand prints.

Clojure code is as dense if not denser than Perl. Scary sigils replaced by even scarier parens. Please note that my Perl is way too dense for some, although still very far from a healthy Perl-golf level.

EDITED

In Clojure, using the strict mapv increased the time of the CPU part, and slightly dropped it for the printing.

EDITED 2

Memory

ClojurePerl
424 MB2 MB

I ask the Clojure police again to evaluate now my lazy sequence of idiomatic/idiotic lines.

Posted Jul 21, 2017, 10:07 PM UTC · edited

1749
Loading comments…