这是代码,它按预期工作:
(let ((previous nil))
(defun vector-int (&optional arg i n (v (if (< n 5) :float :int)))
(cond ((and (pointerp arg) i n)
(unless (equal v (car previous))
(setf previous (cons v (v (if (< n 5) :float :int))))
(mem-aref (mem-aref (%vector-int-to-c-array arg) :pointer i) (cdr previous) n))
(t nil))))
%vector-int-to-c-array 函数并不重要,但要运行它(来自上面的代码):
(mem-aref (mem-aref (%vector-int-to-c-array arg) :pointer i) :int n)
一百万次运行需要 0.5 秒。如果我运行该vector-int
函数,则需要 0.68 秒,而没有(v (if (< n 5) :float :int))
参数和“除非”s 表达式。但如上所述,运行 100 万次需要 1.5 秒。我需要 n 虽然是 :float 如果 n 小于 5 或 :int 如果 n 是 5 或更大。谁能帮我将这段代码加速到不超过 0.68 秒,运行 100 万次。唯一需要注意的是我需要保留 cond 语句..因为我将这段代码缩减为显示在这里,并且在这部分加速后我还有其他条件要添加,当我vector-int
再次运行时,我必须能够更改 n 并拥有更新后的 n 会自动计算。