Clojure阵列的性能问题

dby*_*rne 5 arrays performance clojure

这段代码非常慢.从我上网本上的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))
Run Code Online (Sandbox Code Playgroud)

相反,这段代码运行得非常快:

(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)))
Run Code Online (Sandbox Code Playgroud)

这是否与分块序列有关?我的第一个例子是错误的吗?

dno*_*len 7

你正在进行Java反思.这篇博文有一个解决方法:

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

  • 显然`*警告反思*'并不总是讲述整个故事...另见http://clj-me.cgrand.net/2009/08/06/what-warn-on-reflection-doesnt - 告诉任您对阵列/ (2认同)