我正在编写一个函数,用于检查两个点是否可以在 2D 网格上看到对方,以进行寻路算法。在分析代码后,我发现它在 clojure.lang.Var.getRawRoot() 中花费了 60% 的时间。为什么这个函数消耗这么多时间,我可以优化它吗?
(defn line-of-sight-helper [^Maze maze [x0 y0] [x1 y1]]
"Determines if there is a line of sight from [x0 y0] to [x1 y1] in maze."
(let [dy (int (- y1 y0))
dx (int (- x1 x0))
sy (int (if (neg? dy) -1 1))
sx (int (if (neg? dx) -1 1))
dy (int (* sy dy))
dx (int (* sx dx))
bias-x (int (if (pos? sx) 0 -1))
bias-y (int (if (pos? sy) 0 -1)) …Run Code Online (Sandbox Code Playgroud)