Prolog SICStus序言:FFI慢,如何快速计算汉明体重?

Prolog SICStus序言:FFI慢,如何快速计算汉明体重?,prolog,ffi,hamming-distance,sicstus-prolog,hammingweight,Prolog,Ffi,Hamming Distance,Sicstus Prolog,Hammingweight,当我运行时,如SICStus Prolog 4.3.2手册所示,并将其运行时与相应的Prolog代码进行比较Y为X+9,我得到了奇怪的计时结果: p(N, X) :- ( true1G, X is N+9, false ; X is N+9 ). q(N, X) :- ( true1G, c1(N,X), false ; c1(N,X) ). true10. true10. true10. true10. true10. true10. true10. true10. true10. t

当我运行时,如SICStus Prolog 4.3.2手册所示,并将其运行时与相应的Prolog代码进行比较
Y为X+9
,我得到了奇怪的计时结果:

p(N, X) :- ( true1G, X is N+9, false ; X is N+9 ). q(N, X) :- ( true1G, c1(N,X), false ; c1(N,X) ). true10. true10. true10. true10. true10. true10. true10. true10. true10. true10. true1k :- true10, true10, true10. true1M :- true1k, true1k. true1G :- true1M, true1k. 即使没有正确的错误处理和对大整数的支持,C代码的运行时间几乎是JITted Prolog代码的4倍。关闭JIT会在一定程度上改变计时数字,但总体情况保持不变

如何在SICStus中加速汉明重量计算? SWI有一个专用的算术函数,但是
SICStus似乎不支持它(目前).

关于从Prolog调用C的速度惩罚:从Prolog到C有一个固定的、相当大的开销。如果C代码对Prolog数据进行处理,必须保存大量Prolog状态。因此,C调用比单个is/2慢得多也就不足为奇了。关于从Prolog调用C的速度惩罚:从Prolog到C有固定的、相当大的开销。如果C代码处理Prolog数据,必须保存大量Prolog状态。因此,C调用比单个is/2慢得多并不奇怪。 | ?- call_time(p(11,X), T_p). X = 20, T_p = 17580 ? ; % Prolog code no | ?- call_time(q(11,X), T_q). X = 20, T_q = 66950 ? ; % C code no
| ?- call_time(p(11,X), T_p).
X = 20, T_p = 19650 ? ;                     % Prolog code
no
| ?- call_time(q(11,X), T_q).               
X = 20, T_q = 55840 ? ;                     % C code
no