Open k-okada opened 4 years ago
Luckily for us the eps=
is still under-optimized (why are we using LESSP even when both arguments have been declared as float?), so we can implement a solution that has both better performance and is type-safe.
/*my-eps=*/
static pointer testF1my_eps_(ctx,n,argv,env)
register context *ctx;
register int n; register pointer argv[]; pointer env;
{ register pointer *local=ctx->vsp, w, *fqv=qv;
numunion nu;
double x,y;
if (n<2) maerror();
if (n>3) maerror();
if (n>=3) { local[0]=(argv[2]); goto testENT11;}
local[0]= loadglobal(fqv[0]);
testENT11:
x = fabs(ckfltval(argv[0]) - ckfltval(argv[1]));
y = ckfltval(local[0]);
local[0] = x<y?T:NIL;
testBLK9:
ctx->vsp=local; return(local[0]);}
(defun foo ()
(dotimes (i 100000000)
(eps= 1.0 1.0)))
(defun bar ()
(dotimes (i 100000000)
(my-eps= 1.0 1.0)))
(defun bar-int ()
(dotimes (i 100000000)
(my-eps= 1 1.0)))
(defun bar-int-int ()
(dotimes (i 100000000)
(my-eps= 1 1)))
1.irteusgl$ (my-eps= 1 1.0)
t
2.irteusgl$ (my-eps= 1 1.1)
nil
3.irteusgl$ (bench (foo))
;; time -> 2.63216[s]
nil
4.irteusgl$ (bench (bar))
;; time -> 2.31267[s]
nil
5.irteusgl$ (bench (bar-int))
;; time -> 2.76027[s]
nil
6.irteusgl$ (bench (bar-int-int))
;; time -> 3.43548[s]
nil
The time for the optimized and unsafe code was (bench (bar)) ;; time -> 1.92481[s]
.
Compiling with -O3
made it even faster, with 1.93313[s]
for the type-check and 1.64588[s]
for the unsafe.
となります.これは, https://github.com/euslisp/EusLisp/blob/41c497d0befb29561ab23e664a1c3a51eb17fc16/lisp/geo/geopack.l#L197-L199 で
(declare (type float m n eps))
のお陰で,通常ならとコンパイルされるところが
となっているからのようです.ちなみにこの効果がどれぐらいあるか,ですが, https://gist.github.com/k-okada/39732bdcbf44b485378612dbc39ecb9d を実行してみると1.3-1.8 倍ぐらい速くなっています.
そもそもは型チェックをしない代わりに高速なコードを生成するのが目的なので, すこし対応としてはずれますが, エラーチェックをする,あるいは,方をチェックする,という方法ですが, https://github.com/euslisp/EusLisp/blob/master/lisp/c/eus.h#L818-L822 を使って
とすると,
となり,110-150%程時間が掛かるようです. ちゃと仕様どおり引数に実数を入れていても110%程になる,ということで, これをどう見るか?ワーニングを出すものと,型をキャストするコードにはそこまで速度は変わらず, 結局,型をキャストするかどうか?するならワーニングを出すべきか,という判断になりそうです.