On Oct 8, 7:53 am, "Simo Melenius" <[EMAIL PROTECTED]> wrote:
> 2008/10/8 CuppoJava <[EMAIL PROTECTED]>:
>
>
>
> > I'm very new to clojure, and I'm just wondering what's the most
> > efficient way of creating a vector of zeroes.
> ...
> > It looks pretty bad to me. My java code is more terse than this.
> > Thanks for your help.
>
> I would write something like this to create a vector of N zeroes:
>
> (vec (map (fn [_] 0) (range N)))
>
> but I had no idea whether it's fast or not. Thus, I compared this with
> the solutions other people posted and ran each variant inside both
> (time) and (doall), and repeated the runs 100 times to get an average.
> Here are the results, with averaged time in milliseconds per one
> evaluation:
>
> 33ms: (vec (map (fn [_] 0) (range 100000)))
> 48ms: (vec (replicate 100000 0))
> 56ms: (into [] (replicate 100000 0)))
>
> I'm kind of surprised -- I'd expect map to cost more.
>
The best way to do this is definitely:
(vec (replicate 100000 0))
Because it is the simplest and clearest. Also, vec should be preferred
over (into [] ...), as it yields a flat lazily persistent vector.
However, your timings for map vs replicate are likely to change, and
map is much less clear.
> Further, I would be interested in profiling my Clojure code in order
> to learn what's fast and what's slow. If anyone knows something better
> than using (time) I'd appreciate telling me about it, please. Didn't
> find anything apparent on Google. Would Java profilers be much help
> wrt. Clojure?
>
Java profilers do work with Clojure. I've had success with YourKit.
That said, heed all advice about premature optimization.
Rich
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---