2018-09-03-01-applyを追加したいのだけど-2
>> Site top >> weblog >> 月別アーカイブ >> 2018年09月のlog >> 2018-09-03-01-applyを追加したいのだけど-2
最終更新日付:2018/09/03 21:58:55
applyを追加したいのだけど-2
2018 年 09 月 03 日
昨日書いたこと、もう少しだけ速くできそうだ。
ポイントは、stl:tuple の要素数で分岐した先で、配列の要素数を含めて宣言しなおしてやること。これによって、どうやら配列のインデックスが範囲内かどうかのチェックをいちいちしないで済ませることができるみたい。どんどんコードが読み難くなるが、以下。
(locally (declare (optimize speed)) (defun apply-test-4 (fnc tpl) (let ((arr (stl::__inner-array tpl))) (declare (type simple-vector arr)) (let ((cnt (length arr))) (declare (type fixnum cnt)) (case cnt (2 (locally (declare (type (simple-vector 2) arr)) (cl:funcall fnc (svref arr 0) (svref arr 1)))) (3 (locally (declare (type (simple-vector 3) arr)) (cl:funcall fnc (svref arr 0) (svref arr 1) (svref arr 2)))) (4 (locally (declare (type (simple-vector 4) arr)) (cl:funcall fnc (svref arr 0) (svref arr 1) (svref arr 2) (svref arr 3)))) (t (cl:apply fnc (coerce arr 'cl:list))))))))
で、昨日書いたのも含めて、これらを比較してみる。
(locally (declare (optimize speed)) (defun test-func (a b c) (declare (type fixnum a b c)) (the fixnum (+ a (the fixnum (+ b c)))))) (let ((tpl (stl:make_tuple 111 222 333))) (dolist (apply-fnc (list #'apply-test-1 #'apply-test-2 #'apply-test-3 #'apply-test-4)) (time (dotimes (i 1000000) (funcall apply-fnc #'test-func tpl)))))
結果は以下の通り。一番最後のが今日追加したやつだな。より良くなっていると判断してよさそう。
=> Evaluation took: 0.048 seconds of real time 0.047332 seconds of total run time (0.047332 user, 0.000000 system) [ Run times consist of 0.005 seconds GC time, and 0.043 seconds non-GC time. ] 97.92% CPU 122,360,043 processor cycles 47,988,320 bytes consed Evaluation took: 0.017 seconds of real time 0.016996 seconds of total run time (0.016996 user, 0.000000 system) 100.00% CPU 44,083,672 processor cycles 0 bytes consed Evaluation took: 0.013 seconds of real time 0.013092 seconds of total run time (0.013092 user, 0.000000 system) 100.00% CPU 33,959,700 processor cycles 0 bytes consed Evaluation took: 0.011 seconds of real time 0.011254 seconds of total run time (0.011254 user, 0.000000 system) 100.00% CPU 29,189,440 processor cycles 0 bytes consed
ちなみに、SBCL 1.4.5 での結果だ。そろそろ思いつくこともないので、この件はこれで確定してしまおうかと思っている‥‥‥。
コメント
このページのタグ
Page tag : STLとその移植
Page tag : Common Lisp
Copyright(C) 2005-2019 project-enigma.
Generated by CL-PREFAB.