> On Jul 21, 5:51 am, Jan Rychter <j...@rychter.com> wrote: >> Is there a way to get the size of a data structure in Clojure? >> >> I've been wondering how much more space-efficient vectors are than >> lists, but I don't know how to measure it.
Stuart Sierra <the.stuart.sie...@gmail.com> writes: > Short answer: no, because Java has no sizeof operator. > > You can use Java profiling tools to examine the memory usage of your > app if needed. > > Since all of clojure's data structures are persistent (reusable), they > won't necessarily meet your expectations for memory usage based on the > standard definitions of vector/list/etc. A clojure vector is *not* > just an array. A clojure list is *not* just a linked list. The names > refer to the performance characteristics (vectors allow random access, > lists allow linear access) rather than the implementation. In fact, > most of Clojure's data structures are variants of trees, and they are > quite efficient. > > If you are concerned about memory usage, I recommend you write a > prototype of your app using Clojure's built-in structures first. If > that uses too much memory, try refactoring your code to use Java > arrays or some other Java data structure optimized for your use case. I should have described the problem better. In my case, I'm not considering using native Java data structures. I have stacks implemented on top of Clojure vectors and Clojure lists. I do not care that much about random access, but I do care about O(1) count operation (which both lists and vectors support). I measured the performance in a real-life scenario and it is nearly identical for vectors and lists (lists are actually slightly faster). What I would like to know is given the same amount of data, which structure will be more memory-efficient? I realize this is not an obvious thing to measure, and I did look at the article that describes a solution for Java (http://www.javaworld.com/javaworld/javaqa/2003-12/02-qa-1226-sizeof.html). I've been wondering if something similar has been implemented for Clojure data structures. --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 -~----------~----~----~----~------~----~------~--~---