Creating a n-length vector filled with zeroes?

4,368 views
Skip to first unread message

CuppoJava

unread,
Oct 7, 2008, 11:35:57 PM10/7/08
to Clojure
Hi,
I'm very new to clojure, and I'm just wondering what's the most
efficient way of creating a vector of zeroes.

This is my current code:
(loop [row []
i 0]
(if (= i cols)
row
(recur (assoc row i 0) (inc i))))

It looks pretty bad to me. My java code is more terse than this.
Thanks for your help.
-Cuppo

Parth Malwankar

unread,
Oct 8, 2008, 7:29:47 AM10/8/08
to Clojure


On Oct 8, 8:35 am, CuppoJava <patrickli_2...@hotmail.com> wrote:
> Hi,
> I'm very new to clojure, and I'm just wondering what's the most
> efficient way of creating a vector of zeroes.

This is one way.

user=> (vec (replicate 10 0))
[0 0 0 0 0 0 0 0 0 0]

also

user=> (into [] (replicate 10 0))
[0 0 0 0 0 0 0 0 0 0]

At the REPL you could use (doc <function_name>) to get help on the
function.
There are a bunch of these useful functions like repeat,
replicate, repeatedly etc.

Parth

James Reeves

unread,
Oct 8, 2008, 7:29:53 AM10/8/08
to Clojure
On Oct 8, 4:35 am, CuppoJava <patrickli_2...@hotmail.com> wrote:
> I'm very new to clojure, and I'm just wondering what's the most
> efficient way of creating a vector of zeroes.

I'm not sure about efficiency, but I'd write it like:

(vec (take cols (constantly 0)))

- James

Simo Melenius

unread,
Oct 8, 2008, 7:53:31 AM10/8/08
to clo...@googlegroups.com
2008/10/8 CuppoJava <patrick...@hotmail.com>:

> 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.

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?


br,
S

--
() Today is the car of the cdr of your life.
/\ http://arc.pasp.de/

Rich Hickey

unread,
Oct 8, 2008, 8:29:44 AM10/8/08
to Clojure


On Oct 8, 7:53 am, "Simo Melenius" <simo.melen...@gmail.com> wrote:
> 2008/10/8 CuppoJava <patrickli_2...@hotmail.com>:
>
>
>
> > 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

CuppoJava

unread,
Oct 9, 2008, 11:00:28 AM10/9/08
to Clojure
Thanks for your help.
(vec (replicate 10000 0)) is much better than what I had before.
Reply all
Reply to author
Forward
0 new messages