On Fri, May 28, 2010 at 12:20 PM, Rubén Béjar <ruben.be...@gmail.com> wrote:

>  Hi again,
>
> I have tried a few more things:
>
> I have done the same in Java but using an array
> of Integers, instead of an array of ints. It just takes
> 78 ms. (more than 16, still far less than 4 secs).
>
> I have also tried with an update function which just returns a
> fixed 1. It still takes some 400 ms. to update de
> data vector of the CA. This is similar to the time
> it is giving me to create a vector of 1's with this function:
> (time (vec (repeat 250000 1))).
>
> I was not expecting Clojure vectors so much slower than
> Java arrays. Is that comparison so "unfair"? Next thing
> I am trying is using Java Vectors in my Java
> implementation...
>
>    Rubén
>

I looked into your code and came up with this: http://gist.github.com/420036

On my machine 500x500 takes about 60-70ms. 2000x2000 takes about ~940ms.

Also, Clojure vectors aren't slow:

(defn vec-repeat [n x]
    (loop [n n v (transient [])]
      (if (zero? n)
        (persistent! v)
        (recur (dec n) (conj! v x)))))

(vec-repeat 250000 1)

Takes 4-7ms

(vec-repeat 2500000 1)

Takes 50-60ms

Transients aren't an advanced topic, they solve the problem of building up
large vectors in a memory/cpu efficient manner.

In very tight loops over primitive data:

1. Spending time accessing Clojure data structures can hurt you
2. Spending time creating temporary data structures can hurt you
3. Inline the most called bits.

Fast Clojure code that works with arrays need not look ugly (checkout amap
and areduce). Also macros can alleviate the tedious typing of type hints.

David

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to