Skip to content

Instantly share code, notes, and snippets.

@miyamuko
Created August 20, 2010 11:55
Show Gist options
  • Save miyamuko/540143 to your computer and use it in GitHub Desktop.
Save miyamuko/540143 to your computer and use it in GitHub Desktop.
ファイルの内容を全部読む処理のベンチマーク #xyzzy
;; ファイルの内容を全部読む処理のベンチマーク #xyzzy
;;
;; 参考
;;
;; * SLURP-FILE Performance Comparison
;; http://cybertiggyr.com/gene/sfpc/
;; * Slurping a file in Common Lisp | The Lambda meme - all things Lisp, and more
;; http://www.ymeme.com/node/83
(defun slurp-buffer (filename)
(let ((buf (create-new-buffer "slurp")))
(unwind-protect
(with-set-buffer
(set-buffer buf)
(insert-file-contents filename)
(buffer-substring (point-min) (point-max)))
(delete-buffer buf))))
(defun slurp-read-into (file)
(let* ((len (file-length file))
(buf (make-vector len :element-type 'character :fill-pointer 0)))
(with-open-file (s file :direction :input)
(read-into buf s))))
(defun slurp-read-line (file)
(with-open-file (in file :direction :input)
(with-output-to-string (out)
(loop
(multiple-value-bind (line nl)
(read-line in nil :eof)
(when (eq line :eof)
(return))
(write line :stream out)
(unless nl
(write-char #\LFD out)))))))
;; 実行結果
;; benchmark は http://gist.github.com/507687 と同じ。
;; 上から 1) 3 回分の処理速度、2) 3 回分の平均、3) 実行結果(今回は常に nil)
(setf *file* (merge-pathnames "XTAGS" (si:system-root)))
(format nil "~:D" (file-length *file*))
"1,201,715"
(benchmark
(slurp-buffer *file*)
nil)
(0.031 0.047 0.031)
0.03633333
nil
(benchmark
(slurp-read-into *file*)
nil)
(0.25 0.25 0.234)
0.2446667
nil
(benchmark
(slurp-read-line *file*)
nil)
(30.375 29.906 29.907)
30.06267
nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment