代码之家  ›  专栏  ›  技术社区  ›  dbyrne

Clojure数组的性能问题

  •  5
  • dbyrne  · 技术社区  · 14 年前

    这段代码是 非常 慢。从我上网本上的slime repl执行需要几分钟。

    (def test-array (make-array Integer/TYPE 400 400 3))
    
    (doseq [x (range 400), y (range 400), z (range 3)]
       (aset test-array x y z 0))
    

    相反,此代码运行速度非常快:

    (def max-one (apply max (map (fn [w] (apply max (map #(first %) w))) test-array)))
    (def max-two (apply max (map (fn [w] (apply max (map #(second %) w))) test-array)))
    (def max-three (apply max (map (fn [w] (apply max (map #(last %) w))) test-array)))
    

    这与分块序列有关吗?我的第一个例子写错了吗?

    2 回复  |  直到 11 年前
        1
  •  7
  •   dnolen    14 年前

    你正在击中Java反射。此博客文章有一个解决方法:

    http://clj-me.cgrand.net/2009/10/15/multidim-arrays/

        2
  •  0
  •   Mars    11 年前

    您可以通过一个接口core.matrix从四个Clojure Matrix实现中获得更好的性能: at clojars , at github .