再帰関数の比較(proc lua vs proc fcmp)

目的

luaはバイトコードをコンパイルする方式であるため,高い処理速度であることが知られている.
そこでSASのproc luaはどのぐらいの処理速度なのか再帰処理の関数を作成し検証した.
比較対象としてproc fcmpを用いて評価した.

結果

検証結果,proc fcmpがproc luaに比べて約2~3倍速い.(PC環境により異なる.)

結論

proc lua内で再帰処理をする場合はproc fcmpを組み合わせて実装すること.

参考リンク先

  1. たらいまわし関数の内容
    お気楽 Lua プログラミング超入門
  2. proc fcmpの参考コード
    僕の頁 <SASと臨床試験と雑談と>

proc luaで実装した結果

In [1]:
proc lua;
  submit;
	function tak(x, y, z)
	  if x <= y then
	    return z
	  else
	    return tak(tak(x - 1, y, z), tak(y - 1, z, x), tak(z - 1, x, y))
	  end
	end
	var=tak(18, 9, 0)
    print(var)
  endsubmit;
run;
Out[1]:

11   ods listing close;ods html5 file=stdout options(bitmap_mode='inline') device=png; ods graphics on / outputfmt=png;
NOTE: Writing HTML5 Body file: STDOUT
12
13 proc lua;
14 submit
14 ! ;
15 function tak(x, y, z)
16 if x <= y then
17 return z
18 else
19 return tak(tak(x - 1, y, z), tak(y - 1, z, x), tak(z - 1, x, y))
20 end
21 end
22 var=tak(18, 9, 0)
23 print(var)
24 endsubmit;
25 run;
NOTE: Lua initialized.
9
NOTE: PROCEDURE LUA used (Total process time):
real time 0.74 seconds
cpu time 0.73 seconds

26 ods html5 close;ods listing;

27

proc fcmpで実装した結果

In [2]:
proc fcmp outlib=WORK.FUNCDT.CARCALC ;
  function takf(x, y, z); 
  if x <= y then
    return(z);
  else 
    return (takf(takf(x - 1, y, z), takf(y - 1, z, x), takf(z - 1, x, y)));
  endsub; 
run;
quit;

options cmplib=WORK.FUNCDT;
data _null_;
  rc = takf(18, 9, 0);
  put rc=;
run;
Out[2]:

29   ods listing close;ods html5 file=stdout options(bitmap_mode='inline') device=png; ods graphics on / outputfmt=png;
NOTE: Writing HTML5 Body file: STDOUT
30
31 proc fcmp outlib=WORK.FUNCDT.CARCALC ;
32 function takf(x, y, z);
33 if x <= y then
34 return(z);
35 else
36 return (takf(takf(x - 1, y, z), takf(y - 1, z, x), takf(z - 1, x, y)));
37 endsub;
38 run;
NOTE: Function takf saved to WORK.FUNCDT.CARCALC.
NOTE: PROCEDURE FCMP used (Total process time):
real time 0.02 seconds
cpu time 0.00 seconds

39 quit;
40
41 options cmplib=WORK.FUNCDT;
42 data _null_;
43 rc = takf(18, 9, 0);
44 put rc=;
45 run;
rc=9
NOTE: DATA statement used (Total process time):
real time 0.32 seconds
cpu time 0.32 seconds

46 ods html5 close;ods listing;

47