我试图通过使用优化和 fixnums 从一个小的二次求解器中获得更高的速度。这是我的代码:
1: (defun solve-x (d)
2: (declare (optimize (speed 3))
3: (type fixnum d))
4: (let ((x 1) (y 1))
5: (declare (type fixnum x y))
6: (loop while (/= (- (* x x) (* d y y)) 1) do
7: (if (> (- (* x x) (* d y y)) 1)
8: (incf y)
9: (incf x)))
10: (list x y)))
SBCL 编译器似乎无法正确优化第 6 行和第 7 行。我收到很多这样的警告:
forced to do GENERIC-- (cost 10)
unable to do inline fixnum arithmetic (cost 2) because:
The first argument is a (INTEGER 1 21267647932558653957237540927630737409), not a FIXNUM.
The second argument is a (INTEGER
-98079714615416886892398913872502479823289163909206900736
98079714615416886871131265939943825866051622981576163327), not a FIXNUM.
The result is a (VALUES
(INTEGER
-98079714615416886871131265939943825866051622981576163326
98079714615416886913666561805061133780526704836837638145)
&OPTIONAL), not a (VALUES FIXNUM &REST T).
unable to do inline (signed-byte 64) arithmetic (cost 5) because:
The first argument is a (INTEGER 1 21267647932558653957237540927630737409), not a (SIGNED-BYTE
64).
The second argument is a (INTEGER
-98079714615416886892398913872502479823289163909206900736
98079714615416886871131265939943825866051622981576163327), not a (SIGNED-BYTE
64).
The result is a (VALUES
(INTEGER
-98079714615416886871131265939943825866051622981576163326
98079714615416886913666561805061133780526704836837638145)
&OPTIONAL), not a (VALUES (SIGNED-BYTE 64) &REST T).
etc.
不知道在哪里继续。我已经尝试在乘法、除法和减法周围插入“fixnum”,但它只会变得更糟。
任何想法,如何快速做到这一点?
最佳答案
如果您确定数字不会在任何时候溢出,您可以将 (SAFETY 0)
添加到优化中。还要在计算周围添加 (THE FIXNUM ...)
以告诉 SBCL 您希望将结果视为 fixnum。三个参数 *
应拆分为两个单独的调用。
您的代码当前正在循环中计算 (- (* x x) (* d y y))
两次。您应该将其分配给一个变量。另请注意,由于循环中只有 X
或 Y
发生变化,因此没有必要再次计算另一部分(我不知道那些计算是什么,所以我只将它们称为 FOO
、 BAR
和 QUUX
)。
(defun solve-x (d)
(declare (optimize (speed 3) (safety 0) (debug 0))
(type fixnum d))
(let ((x 1) (y 1))
(declare (type fixnum x y))
(loop with foo of-type fixnum = (* x x)
with bar of-type fixnum = (* (the fixnum (* d y)) y)
for quux of-type fixnum = (- foo bar)
while (/= quux 1)
do (if (> quux 1)
(setf y (1+ y)
bar (* (the fixnum (* d y)) y))
(setf x (1+ x)
foo (* x x))))
(list x y)))
为了避免两次编写公式,您可以使用
#n=
阅读器宏。 X
和 Y
也可以作为 &AUX
变量移动到参数列表中,以摆脱 LET
和第二个 DECLARE
。(defun solve-x (d &aux (x 1) (y 1))
(declare (optimize (speed 3) (safety 0) (debug 0))
(type fixnum d x y))
(loop with foo of-type fixnum = #1=(* x x)
with bar of-type fixnum = #2=(* d (the fixnum (* y y)))
for quux of-type fixnum = (- foo bar)
while (/= quux 1)
do (if (> quux 1)
(setf y (1+ y)
bar #2#)
(setf x (1+ x)
foo #1#)))
(list x y))
由于
X
和 Y
总是加一,你可以通过增加前一个值来避免一些乘法运算。(defun solve-x (d &aux (x 1) (y 1))
(declare (optimize (speed 3) (safety 0) (debug 0))
(type fixnum d x y))
(loop with foo of-type fixnum = 1
with bar of-type fixnum = d
for quux of-type fixnum = (- foo bar)
while (/= quux 1)
do (if (> quux 1)
(setf bar (+ bar (the fixnum (* d y)))
y (1+ y)
bar (+ bar (the fixnum (* d y))))
(setf foo (+ foo x)
x (1+ x)
foo (+ foo x))))
(list x y))
关于optimization - SBCL:Fixnum 优化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40681075/