小编bsl*_*ski的帖子

总结Clojure比率很慢

我在Clojure中总结了一长串比率,例如:

(defn sum-ratios
  [n]
  (reduce
    (fn [total ind]
      (+
        total
        (/
          (inc (rand-int 100))
          (inc (rand-int 100)))))
    (range 0 n)))
Run Code Online (Sandbox Code Playgroud)

各种n的运行时间是:

  • n = 10 ^ 4 ...... 41 ms
  • n = 10 ^ 6 ...... 3.4 s
  • n = 10 ^ 7 ...... 36秒


(不太精确)替代方案是将这些值加总为双精度:

(defn sum-doubles
  [n]
  (reduce
    (fn [total ind]
      (+
        total
        (double
          (/
            (inc (rand-int 100))
            (inc (rand-int 100))))))
    (range 0 n)))
Run Code Online (Sandbox Code Playgroud)

此版本的运行时为:

  • n = 10 ^ 4 ...... 8.8 ms
  • n = 10 ^ 6 ...... …

optimization clojure

6
推荐指数
1
解决办法
132
查看次数

标签 统计

clojure ×1

optimization ×1