Hi Mr Volkmann,

Here are my result:

user=> (println "\nconj list")

conj list
nil
user=> ; Adds to front.
user=> (build-coll '() (fn [coll i] (conj coll i)) size)
"Elapsed time: 28.09443 msecs"
nil
user=>
user=> (println "\ncons list")

cons list
nil
user=> ; Adds to front.
user=> (build-coll '() (fn [coll i] (cons i coll)) size)
"Elapsed time: 9.616097 msecs"
nil
user=>
user=> (println "\nconj vector")

conj vector
nil
user=> ; Adds to back.
user=> (build-coll [] (fn [coll i] (conj coll i)) size)
"Elapsed time: 30.66506 msecs"
nil
user=>
user=> (println "\ncons vector")

cons vector
nil
user=> ; Adds to front.
user=> (build-coll [] (fn [coll i] (cons i coll)) size)
"Elapsed time: 13.421022 msecs"
nil
user=> (println "\nconj list")

conj list
nil
user=> ; Adds to front.
user=> (build-coll '() (fn [coll i] (conj coll i)) size)
"Elapsed time: 12.922417 msecs"
nil
user=>
user=> (println "\ncons list")

cons list
nil
user=> ; Adds to front.
user=> (build-coll '() (fn [coll i] (cons i coll)) size)
"Elapsed time: 5.386954 msecs"
nil
user=>
user=> (println "\nconj vector")

conj vector
nil
user=> ; Adds to back.
user=> (build-coll [] (fn [coll i] (conj coll i)) size)
"Elapsed time: 21.946047 msecs"
nil
user=>
user=> (println "\ncons vector")

cons vector
nil
user=> ; Adds to front.
user=> (build-coll [] (fn [coll i] (cons i coll)) size)
"Elapsed time: 5.002202 msecs"
nil
user=> (println "\nconj list")

conj list
nil
user=> ; Adds to front.
user=> (build-coll '() (fn [coll i] (conj coll i)) size)
"Elapsed time: 4.008019 msecs"
nil
user=>
user=> (println "\ncons list")

cons list
nil
user=> ; Adds to front.
user=> (build-coll '() (fn [coll i] (cons i coll)) size)
"Elapsed time: 4.179548 msecs"
nil
user=>
user=> (println "\nconj vector")

conj vector
nil
user=> ; Adds to back.
user=> (build-coll [] (fn [coll i] (conj coll i)) size)
"Elapsed time: 4.926285 msecs"
nil
user=>
user=> (println "\ncons vector")

cons vector
nil
user=> ; Adds to front.
user=> (build-coll [] (fn [coll i] (cons i coll)) size)
"Elapsed time: 3.94195 msecs"
nil
user=> (println "\nconj list")

conj list
nil
user=> ; Adds to front.
user=> (build-coll '() (fn [coll i] (conj coll i)) size)
"Elapsed time: 5.206835 msecs"
nil
user=>
user=> (println "\ncons list")

cons list
nil
user=> ; Adds to front.
user=> (build-coll '() (fn [coll i] (cons i coll)) size)
"Elapsed time: 3.958223 msecs"
nil
user=>
user=> (println "\nconj vector")

conj vector
nil
user=> ; Adds to back.
user=> (build-coll [] (fn [coll i] (conj coll i)) size)
"Elapsed time: 6.29202 msecs"
nil
user=>
user=> (println "\ncons vector")

cons vector
nil
user=> ; Adds to front.
user=> (build-coll [] (fn [coll i] (cons i coll)) size)
"Elapsed time: 4.560739 msecs"
nil
user=> (println "\nconj list")

conj list
nil
user=> ; Adds to front.
user=> (build-coll '() (fn [coll i] (conj coll i)) size)
"Elapsed time: 14.811261 msecs"
nil
user=>
user=> (println "\ncons list")

cons list
nil
user=> ; Adds to front.
user=> (build-coll '() (fn [coll i] (cons i coll)) size)
"Elapsed time: 13.438472 msecs"
nil
user=>
user=> (println "\nconj vector")

conj vector
nil
user=> ; Adds to back.
user=> (build-coll [] (fn [coll i] (conj coll i)) size)
"Elapsed time: 17.98685 msecs"
nil
user=>
user=> (println "\ncons vector")

cons vector
nil
user=> ; Adds to front.
user=> (build-coll [] (fn [coll i] (cons i coll)) size)
"Elapsed time: 13.808419 msecs"
nil
user=>

On my computer, cons is faster for the first runs but after warm up
results are more similar. Even some conj tests are a little bit
faster.

At the same time, thank you very much for your nice tutorial.

Stéphane Rousseau





On Mar 6, 8:15 am, Mark Volkmann <[email protected]> wrote:
> On Fri, Mar 6, 2009 at 7:00 AM, Chouser <[email protected]> wrote:
>
> > On Fri, Mar 6, 2009 at 7:55 AM, Mark Volkmann <[email protected]> 
> > wrote:
>
> >> When working on a list, both cons and conj add to the front. In my
> >> tests, cons is considerably faster than conj. I'm trying to figure out
> >> why.
>
> > In my testing they are the same speed.
>
> Here's my code that seems to show that cons is faster than conj for
> both lists and vectors. Am I doing something suspect that is skewing
> my results?
>
> (def size 10000)
>
> (defn build-coll
>   "prints the time required to change the contents of a collection
>    to the result of a given function n times
>    where values from 0 to n-1 are passed to the function"
>   [coll a-fn size]
>   (let [an-atom (atom coll)]
>     (time
>       (dotimes [i size]
>         (reset! an-atom (a-fn @an-atom i))))
>     ;(println @an-atom)
>     ))
>
> (println "\nconj list")
> ; Adds to front.
> (build-coll '() (fn [coll i] (conj coll i)) size)
>
> (println "\ncons list")
> ; Adds to front.
> (build-coll '() (fn [coll i] (cons i coll)) size)
>
> (println "\nconj vector")
> ; Adds to back.
> (build-coll [] (fn [coll i] (conj coll i)) size)
>
> (println "\ncons vector")
> ; Adds to front.
> (build-coll [] (fn [coll i] (cons i coll)) size)
>
>
>
> >> Here's the implementation of conj.
>
> >> (def
> >>  #^{:arglists '([coll x] [coll x & xs])
> >>    :doc "conj[oin]. Returns a new collection with the xs
> >>    'added'. (conj nil item) returns (item).  The 'addition' may
> >>    happen at different 'places' depending on the concrete type."}
> >>  conj (fn conj
> >>        ([coll x] (. clojure.lang.RT (conj coll x)))
> >>        ([coll x & xs]
> >>         (if xs
> >>           (recur (conj coll x) (first xs) (next xs))
> >>           (conj coll x)))))
>
> >> The line for the parameter list [coll x] seems to call itself
> >> recursively with the exact same arguments. How does that ever
> >> terminate?
>
> > You're probably getting confused by the old-style Java interop syntax.
>
> >  (. clojure.lang.RT (conj coll x))
>
> > is the same as:
>
> >  (clojure.lang.RT/conj coll x)
>
> Ah ... you're right! I haven't looked at the old style in a while. It
> seems weird that it can break apart (conj coll x) to get what you show
> above before evaluating it. I much prefer the new style.
>
> --
> R. Mark Volkmann
> Object Computing, Inc.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to