If you need speed, you need to enter the world of primitives and not leave it until you finish. This is useless, starting with the boxed Integer i
, and then converting it to a primitive on each usage site. Perhaps you can make dotimes
produce ints (type hint declaration i
), but not sure. What I know is a loop-recur
with primitive loop vector initializers: (loop [i (int 0)] ... (recur (unchecked-inc i))
. Also, in your example, you have (int 3)
.You need to let
in advance so as not to repeat the decompression at each iteration.
By the way, you can use (int-array (range 1000000))
to create your initialized array and just (int-array 1000000)
for empty.
UPDATE
As with Clojure 1.3, with improved support for primitives, most of what I wrote above no longer applies. dotimes
already uses primitive arithmetic, so all you need to write to get full performance is
(dotimes [i (alength ^ints xs)] (aset ^ints ys i (unchecked-multiply (aget ^ints xs i) 3)
Basically, no int
constructors are needed and use unchecked-multiply
.
source share