続・数値から数字のリストへの変換
shiro さんからのありがたいコメントにより,digit-char-p という
関数が役をしてくれるということを知ったので,早速変更してみた。
ついでに,time 関数で何サイクルかかるか見てみた。
以下は,キャストの皆様。
;; 1番。最初のものからの変更点は,digit-char-p,write-to-string を ;; 使用するようにした点。 (defun num-to-list (num) (mapcar (lambda (ch) (digit-char-p ch)) (coerce (write-to-string num) 'list))) ;; 2番。digit-char-p を使用するようにした。 (defun num-to-list-with-format (num) (mapcar (lambda (ch) (digit-char-p ch)) (coerce (format nil "~A" num) 'list))) ;; 最初に載せたやつ。 (defun old-num-to-list (num) (mapcar (lambda (ch) (- (char-code ch) (char-code #\0))) (coerce (format nil "~A" num) 'list)))
以下,結果。正しくは,何千回か実行したりごにょごにょして平均を
とったりするんだろうけど,いい加減な人間なので,その辺を華麗にパスッ!
予想通り勝利者は,digit-char-p,write-to-string を使用したものでした〜。
CL-USER> (time (num-to-list 1234567890)) Evaluation took: 0.000 seconds of real time 0.000000 seconds of total run time (0.000000 user, 0.000000 system) 100.00% CPU 32,688 processor cycles 0 bytes consed (1 2 3 4 5 6 7 8 9 0) CL-USER> (time (num-to-list-with-format 1234567890)) Evaluation took: 0.000 seconds of real time 0.000000 seconds of total run time (0.000000 user, 0.000000 system) 100.00% CPU 38,488 processor cycles 0 bytes consed (1 2 3 4 5 6 7 8 9 0) CL-USER> (time (old-num-to-list 1234567890)) Evaluation took: 0.000 seconds of real time 0.000000 seconds of total run time (0.000000 user, 0.000000 system) 100.00% CPU 43,592 processor cycles 0 bytes consed (1 2 3 4 5 6 7 8 9 0)
ちなみに,別の方法として,10で割ったあまりを cons てった場合とどっちが
早いのだろうか?
そのうちやってみるかも。