Hello,
I've just started learning Clojure, so I'm trying to figure out the
correct way of doing things. I've been trying to create and 'modify' a
large vector for an online programming exercise, but I'm running into
some performance issues.
Any general tips would be appreciated!
Firstly, I'm creating a vector of booleans like this:
(defn vector-of [n value]
(vec (for [_ (range n)] value)))
It takes quite a long time for large values of n, though:
user=> (time (dorun (vector-of 1e7 true)))
"Elapsed time: 6734.509528 msecs"
I tried a few other combinations of things, e.g. (apply (vector
(for ...))), (take n (cycle [value])), but they were even slower. Am I
doing it wrong?
Secondly, I'm iterating across one of these large vectors using
something like the following (contrived) function:
(defn set-flags [v]
(loop [i 0
v v]
(if (< i (count v))
(recur (inc i) (assoc v i false))
v)))
user=> (let [v (vector-of 1e7 true)] (time (dorun (set-flags v))))
"Elapsed time: 15563.916114 msecs"
Am I missing anything obvious here? That seems like a really long time
to me.
Any help greatly appreciated! Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---