--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
(defn array-max [^doubles arr]
(let [len (alength arr)]
(loop [m Double/NEGATIVE_INFINITY indx 0]
(if (< indx len)
(recur (max m (aget arr indx)) (unchecked-inc indx))
m))))
user=> (let [vs (amap (double-array 1280000) idx ret (Math/random))]
(time (array-max vs)))
"Elapsed time: 3.719835 msecs"
user=> (source areduce)
(defmacro areduce
"Reduces an expression across an array a, using an index named idx,
and return value named ret, initialized to init, setting ret to the
evaluation of expr at each step, returning ret."
{:added "1.0"}
[a idx ret init expr]
`(let [a# ~a]
(loop [~idx 0 ~ret ~init]
(if (< ~idx (alength a#))
(recur (unchecked-inc ~idx) ~expr)
~ret))))
It's just a macro, and so typehinting is going to play a factor. For example, with areduce and a type hint on the array:
(defn array-max2 [^doubles arr]
(areduce arr idx ret Double/NEGATIVE_INFINITY (max ret (aget arr idx))))
user=> (let [vs (amap (double-array 1280000) idx ret (Math/random))] (time (array-max vs)))
"Elapsed time: 3.314599 msecs"
But with no type hint on arr:
(defn array-max2 [arr]
(areduce arr idx ret Double/NEGATIVE_INFINITY (max ret (aget arr idx))))
user=> (let [vs (amap (double-array 1280000) idx ret (Math/random))] (time (array-max2 vs)))
"Elapsed time: 35612.919192 msecs"
Without a typehint on the arr argument, I also do get boxed math and reflection warnings:
Reflection warning, /private/var/folders/0k/xj_drd990xxf4q99n2bdknrc0000gn/T/form-init1595291808747030463.clj:2:3 - call to static method alength on clojure.lang.RT can't be resolved (argument types: unknown).
Boxed math warning, /private/var/folders/0k/xj_drd990xxf4q99n2bdknrc0000gn/T/form-init1595291808747030463.clj:2:3 - call: public static boolean clojure.lang.Numbers.lt(long,java.lang.Object).
Reflection warning, /private/var/folders/0k/xj_drd990xxf4q99n2bdknrc0000gn/T/form-init1595291808747030463.clj:2:58 - call to static method aget on clojure.lang.RT can't be resolved (argument types: unknown, int).
Boxed math warning, /private/var/folders/0k/xj_drd990xxf4q99n2bdknrc0000gn/T/form-init1595291808747030463.clj:2:49 - call: public static java.lang.Object clojure.lang.Numbers.max(double,java.lang.Object).
form-init1595291808747030463.clj:2 recur arg for primitive local: ret is not matching primitive, had: Object, needed: double
Auto-boxing loop arg: ret
Reflection warning, /private/var/folders/0k/xj_drd990xxf4q99n2bdknrc0000gn/T/form-init1595291808747030463.clj:2:3 - call to static method alength on clojure.lang.RT can't be resolved (argument types: unknown).
Boxed math warning, /private/var/folders/0k/xj_drd990xxf4q99n2bdknrc0000gn/T/form-init1595291808747030463.clj:2:3 - call: public static boolean clojure.lang.Numbers.lt(long,java.lang.Object).
Reflection warning, /private/var/folders/0k/xj_drd990xxf4q99n2bdknrc0000gn/T/form-init1595291808747030463.clj:2:58 - call to static method aget on clojure.lang.RT can't be resolved (argument types: unknown, int).
Boxed math warning, /private/var/folders/0k/xj_drd990xxf4q99n2bdknrc0000gn/T/form-init1595291808747030463.clj:2:49 - call: public static java.lang.Object clojure.lang.Numbers.max(java.lang.Object,java.lang.Object).
Lesson learned here for me is that only use java array when absolutely necessary. I always thought since it's primitive array, it should be the fastest. Apparently not!
Just wondering though, is there a faster way to load an array than this way? https://github.com/malloc82/imaging/blob/45475b99f564b1ac77e668e04b91cb9c01a096d7/src/imaging/dicom.clj#L138the data file I'm trying to read from contains text based pixel values.