昨日の続きでたらい回しを続けてみました。ていうか、起きてからこんなことする正月ってどうかしているよ。
ソースコード
tarai-normal は比較用ですよ。
;;tarai var with type fixnum, function withfixnum (defun tarai-fixnum (x y z) (declare (fixnum x y z)) (if (<= x y) y (the fixnum (tarai-fixnum (the fixnum (tarai-fixnum (1- x) y z)) (the fixnum (tarai-fixnum (1- y) z x)) (the fixnum (tarai-fixnum (1- z) x y)))))) ;;tarai without type (defun tarai-normal (x y z) (if (<= x y) y (tarai-normal (tarai-normal (1- x) y z) (tarai-normal (1- y) z x) (tarai-normal (1- z) x y)))) ;;; Driver (tarai-fixnum 20 10 5) (quit)
clispでやってみた。
CL-USER> (time (tarai-fixnum 20 10 5)) Real time: 355.38486 sec Run time: 355.04617 sec Space 0 Byte 20 CL-USER> (time (tarai-normal 20 10 5)) Real time: 355.72623 sec. Run time: 355.349 sec. Space: 0 Bytes 20
あれあれ。コンパイルしていなかったら、型チェックなしの方がはやいって?
よくわからない。いやそもそもコンパイルのオプションを高速化にしていないから?
eclでやってみた。
term | lisp(ecl) | lisp(型付き) | c(apple gcc) | c(clang最速) | c(gcc最速) | haskell |
real | 1m21.615s | 1m44.635s | 0m39.786s | 0m15.805s | 0m15.140s | 0m0.099s |
user | 1m21.172s | 1m44.365s | 0m89.315s | 0m15.705s | 0m14.721s | 0m0.001s |
sys | 0m 0.086s | 0m 0.080s | 0m 0.036s | 0m 0.012s | 0m 0.005s | 0m0.004s |
型付きにしたら早くなるんじゃ??
なにか間違っているらしい。うーん、コンパイルのオプションかなぁ。
ecl で コンパイルのオプションをやってみた
eclのトップレベルで、
(declaim (optimize (speed 3) (compilation-speed 0) (safety 0) (debug 0)))
したら、Safety=2 Space=0 Speed=3 Debug=0になったけど、まあいいか。
Driverにこれを追加
;;; Driver (tarai-fixnum 20 10 5) (quit)
そのままコンパイルして実行
time ./taraifixnum.x
real 1m44.627s
user 1m44.384s
sys 0m0.086s
そんなばかな....型付き+高速化で遅くなるだと??
clispで型付き+コンパイルオプションでやってみよう!
(declaim (optimize (speed 3) (compilation-speed 0) (safety 0) (debug 0)))
宣言してから、load+compile
(load (compile-file "taraifixnum.lisp")) ;;-> slime終了
そんなのひどいよ!!あんまりだよ!!
あ、(quit)つけてた...orz
良く考えれば、integerじゃね?
(defun tarai-integer (x y z) (declare (integer x y z)) (if (<= x y) y (the integer (tarai-integer (the integer (tarai-integer (1- x) y z)) (the integer (tarai-integer (1- y) z x)) (the integer (tarai-integer (1- z) x y)))))) ;;; Driver (tarai-integer 20 10 5) (quit)
real 1m30.232s
user 1m30.021s
sys 0m0.084s
あれれ??