(deftype Tree [n l r]
  clojure.lang.Indexed
  (nth [this idx]
    (nth this idx nil))
  (nth [_ idx not-found]
    (case idx
      0 n
      1 l
      2 r
      not-found)))

(defn generate-tree [[h & t :as coll]]
  (when (seq coll)
    (let [lr (generate-tree t)] (Tree. h lr lr))))

(defn to-list
  ([node] (persistent! (to-list node (transient []))))
  ([[v l r :as node] acc]
     (if-not (nil? node)
       (let [lacc (to-list l acc)
             _   (conj! lacc v)
             racc (to-list r lacc)]
         racc)
       acc)))

(defn tree [n]
  (generate-tree (range 1 n)))

(let [t (tree 21)]
   (dotimes [_ 5]
     (time
      (dotimes [_ 1]
        (count (to-list t))))))

Takes ~53ms on my machine.

David

On Mon, Jul 23, 2012 at 5:00 PM, Alexander Semenov <bohtva...@gmail.com> wrote:
> No, the results are pretty the same (+-20ms).
>
> In fact I tried using https://github.com/hugoduncan/criterium but it dies
> with 'out of memory' (:reduce-with option doesn't help), so I gave up.
>
>
> On Monday, July 23, 2012 11:54:28 PM UTC+3, lpetit wrote:
>>
>>
>> Does the timing stabilize to a lesser value if you execute it multiple
>> times (as they did in the scala test) :
>>
>> (dotimes [ _ 10] (time (to-list tree)) :done)
>>
>> ?
>
> --
> 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

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