3 ms·
I love that this Lisp version from a comment at lobste.rs (https://lobste.rs/s/xz5l8t/one_program_written_python_go_rust#c_lxm4vf https://lobste.rs/s/xz5l8t/one
by zeveb 7y ago
I love that this Lisp version from a comment at lobste.rs (https://lobste.rs/s/xz5l8t/one_program_written_python_go_rust#c_lxm4vf https://lobste.rs/s/xz5l8t/one_program_written_python_go_rus...) runs in less than half the time of Rust, and is more readable to boot:
(declaim (ftype (function (string string) double-float) img-diff))
(defun img-diff (first-file-name second-file-name)
(declare (optimize (speed 3) (safety 0) (debug 0) (space 0)))
(let ((im1 (png:decode-file first-file-name))
(im2 (png:decode-file second-file-name)))
(declare ((SIMPLE-ARRAY (UNSIGNED-BYTE 8) (* * *)) im1 im2))
(/ (loop
for i fixnum below (array-total-size im1)
summing (abs (- (row-major-aref im1 i)
(row-major-aref im2 i))) fixnum)
(* (ash 1 (png:image-bit-depth im1)) (array-total-size im1))
1.0))) ; Convert rational to float
(img-diff "file1.png" "file2.png")
- kiaulen 7y agoLess than half the time is provable. More readable is a bit of a stretch. What does declaim mean? What about ftype? What is the (* * *) in the array declaration? What is ash? I've read through some of CLtL and the rust book both, but none of those are constructs I've come across. Also (this doesn't matter in practice due to rainbow parens for almost every editor), it's really hard to read lisp code without syntax highlighting. Rust isn't super easy, but I don't have to count braces to see what lines up.
- deleted 7y ago[deleted]