5

This piece of code is very slow. Execution from the slime-repl on my netbook takes a couple minutes.

(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))

Conversely, this code runs really fast:

(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)))

Does this have something to do with chunked sequences? Is my first example just written wrong?

2 Answers 2

7

You're hitting Java reflection. This blog post has a workaround:

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the link. I thought it might be reflection, but I am not getting any reflection warnings? Any idea why not?
Apparently *warn-on-reflection* doesn't always tell the whole story... See also clj-me.cgrand.net/2009/08/06/…
With Clojure 1.2 you should get more reflection warnings on arrays.
0

You might get better performance from one of the four Clojure matrix implementations available via a single interface core.matrix: at clojars, at github.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.