On Wed, Jun 24, 2009 at 7:53 AM, Richard Newman <holyg...@gmail.com> wrote:

>
> > Even with the optimization, sort somehow beats top for speed. It looks
> > like top is best used to avoid major memory consumption for long seqs;
> > if you have the memory and need the speed, sort's better.
>
> This is an interesting characteristic I've noticed in sorting code. In
> the past (I'm thinking of one Common Lisp app in particular) I've
> spent time carefully crafting tree-based accumulators to collect
> values into a sorted collection... only to find that it was slower
> than just collecting the whole damn list and sorting it, even for very
> large collections. Seems all those tree/map operations take time, and
> library sort functions are fast!



Then use sort!

Prototype implementation:
(defn top2 [n comparator coll]
  (let [qtop #(take n (sort comparator (concat %1 %2)))]
    (reduce qtop (partition (* 10 n) coll))))

user=> (time (top2 10 #(> %1 %2) (range 5000000)))
"Elapsed time: 2990.945916 msecs"
(4999999 4999998 4999997 4999996 4999995 4999994 4999993 4999992 4999991
4999990)
user=> (time (take 10 (sort #(> %1 %2) (range 5000000))))
"Elapsed time: 4900.345365 msecs"
(4999999 4999998 4999997 4999996 4999995 4999994 4999993 4999992 4999991
4999990)



-- 
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.blogspot.com/ (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