Thanks to everyone who replied in this thread. I am impressed by the signal-to-noise ration on this list and by the quality of replies. I will try to reply to everyone in a single post, trying to summarize.
> On Jul 13, 11:15 am, Jan Rychter <j...@rychter.com> wrote: >> I've been trying to implement stacks using vectors in Clojure. In case >> you wonder why, it's because I need stacks with a fast item count >> operation. Rob <rob.nikan...@gmail.com> writes: > If that's the only reason, you don't have to use vectors. The > following page says that 'count' for lists is O(1) Indeed -- I missed that, and my experiments seemed to indicate that calling (count) really slows things down. It turns out it was because my lists were turning into seqs behind my back. Mark Engelberg <mark.engelb...@gmail.com> writes: > Using conj and pop on vectors for stack operations should work just > fine. Don't use subvec though; nested subvecs will really start to > slow things down. I went ahead and implemented my stacks using both lists and vectors, then did some careful benchmarking. It turns out there is no discernible performance difference. I am not sure what you mean about subvecs, though -- I currently use them for dropn and rot operations, and I don't know how to avoid using them: (defmacro stack-dropn [stack n] `(let [stack# ~stack] (subvec stack# 0 (- (stack-count ~stack#) ~n)))) (defmacro stack-rot [stack] `(let [stack# ~stack] (if (> (stack-count stack#) 1) (apply conj (vector (peek stack#)) (subvec stack# 0 (dec (count stack#)))) stack#))) (incidentally, if anyone can suggest a better rot implementation, I would be very grateful -- rot "rotates" the stack, putting the last element of the vector first) So, to summarize -- my real performance problem is that my lists were becoming lazy seqs without me knowing. It turns out you REALLY REALLY have to know the difference between a list and a seq. And, since performance of lists and vectors in my case is identical, I don't know which one to choose now for my stacks? --J. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---