Last active
May 20, 2019 14:48
-
-
Save j3pic/2000fd02c01a2db2fdb3fbcf4dad9ae7 to your computer and use it in GitHub Desktop.
Program to test the performance of Lisp file I/O.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; It is often said that Lisp macros need not be used for optimization purposes. This | |
;; is an exception. SBCL issued the following warning when compiling the MAKE-ARRAY call | |
;; below: | |
;; ; note: unable to optimize because: ELEMENT-TYPE is not constant. | |
;; I don't want to lose the ability to decide which ELEMENT-TYPE to use by using | |
;; an :ELEMENT-TYPE argument, so I have to arrange for this argument to be processed | |
;; at compile time. | |
(defmacro define-test-file-functions () | |
(let* ((element-type* (gensym)) | |
(function-name (gensym)) | |
(template `(defun ,function-name (infile outfile) | |
(declare (optimize (speed 3) (safety 0))) | |
(ignore-errors (delete-file outfile)) | |
(time | |
(with-open-file (in infile :element-type ,element-type*) | |
(with-open-file (out outfile :direction :output :element-type ,element-type*) | |
(let ((buffer (make-array (file-length in) :element-type ,element-type*))) | |
(read-sequence buffer in) | |
(write-sequence buffer out))))))) | |
(types/defs-alist (loop for type in '((unsigned-byte 8) character) | |
for realname = (gensym) | |
collect (cons type (subst (list 'quote type) element-type* | |
(subst realname function-name | |
template)))))) | |
`(progn | |
,@(mapcar #'cdr types/defs-alist) | |
(defparameter *test-file-functions* ',(loop for (type . (defun name)) in types/defs-alist | |
collect (cons type name)))))) | |
(define-test-file-functions) | |
(defmacro test-file (infile outfile &key (element-type '(unsigned-byte 8))) | |
`(time (,(cdr (assoc element-type *test-file-functions* :test #'equal)) ,infile ,outfile))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment