by gibsonf1 · 5y ago · 0 comments
For kjvbible.txt at (unoptimized) 0.365 is 3rd fastest beating C, and 4th faststest beating optimized Go
(would be interesting to see how fast the optimized Lisp would be)
Common Lisp (sbcl.org):
(defmethod performance-count ((path-file string))
(let ((map (make-hash-table :test 'equal)))
(with-open-file (stream path-file :direction :input :if-does-not-exist nil)
(when stream
(loop for line = (read-line stream nil 'end)
until (eq line 'end)
do
(let ((split (split-string #\space (string-downcase line))))
(dolist (word split)
(let ((index (gethash word map)))
(if index
(setf (gethash word map) (incf index))
(setf (gethash word map) 1))))))
(let ((keys (sort (alexandria:hash-table-keys map) (lambda(x y)(> (gethash x map)(gethash y map))))))
(dolist (key keys)
(format t "~A ~A~%" key (gethash key map))))))))
WEB> (time (performance-count "/home/frederick/performance-comparison.txt"))the 4
foo 2
defenestration 1
I suppose you could also recreate all their examples to create your own baseline of runtime performance but that's a lot of work for what seems to be a not-very empirical benchmark (at least to me).
Disclaimer: I did not check runtime complexity of any of the implementations because I didn't really care and skipped straight to the performance results table.
https://github.com/benhoyt/countwords/blob/master/kjvbible.t...
firmament: 10
firmament, 10
genesis 10
version 10
Evaluation took:
2.798 seconds of real time
2.813069 seconds of total run time (2.677126 user, 0.135943 system)
100.54% CPU
8,125,595,437 processor cycles
934,110,048 bytes consedthe 64015
and 51313
of 34634
26879
to 13567
that 12784
in 12503
he 10261
shall 9838
unto 8987
for 8810
i 8708
... Evaluation took:
0.365 seconds of real time
0.370322 seconds of total run time (0.338364 user, 0.031958 system)
101.37% CPU
1,060,005,621 processor cycles
106,297,040 bytes consed
I had a bug in my original code which alphabetized rather than sorted by count, so the sort line should be:((keys (sort (alexandria:hash-table-keys map) (lambda(x y)(> (gethash x map)(gethash y map))))))