I have tried (apply + arr) and (doseq ...). See below.
Is there a way to improve my code?
(def arr (range 20480))
(set! js/arr (clj->js arr))
(js/eval "function sum(arr) {
var total = 0,
i;
for (i = 0; i < arr.length; i++) {
total += arr[i];
}
return total;
}")
(defn my-sum[a]
(def res 0)
(doseq [x a]
(set! res (+ res x )))
res)
(time (js/sum js/arr)) ; less than 1 msec
(time (apply + arr)); around 10 msec
(time (my-sum arr)); around 10 msec
(defn my-sum[a]
(def res 0)
(doseq [x a]
(set! res (+ res x )))
res)
--
Note that posts from new members are moderated - please be patient with your first post.
---
You received this message because you are subscribed to a topic in the Google Groups "ClojureScript" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojurescript/ex2GmRMQCXQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojurescrip...@googlegroups.com.
To post to this group, send email to clojur...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojurescript.
You received this message because you are subscribed to the Google Groups "ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojurescrip...@googlegroups.com.