This can't really be done in a language that transparently handles pointers for you - who's "responsible" for some data that's pointed to by four different pointers?
(let [a [1 2 [1 2]] b [2 [1 2]] c (next a)] (map total-memory [a b c])) What does this return? b and c are structurally identical, but c is taking up less memory because really it's just a pointer into a subvector of a. If you say that c should print the same size as b - just pretend data sharing doesn't exist - then this feature becomes misleading if you use it to compare how much space algorithms require. (defn sum1 [list] (loop [data (into-array Integer/TYPE list) acc 0] (if (zero? (alength data)) acc (recur (into-array Integer/TYPE (next data)) (+ acc (first data)))))) (defn sum2 [list] (loop [data (seq list) acc 0] (if-not data acc (recur (rest data) (+ acc (first data)))))) If you added some code to add up the total memory taken by every object in each iteration of the loop, you would get the wrong results. An array of N ints definitely takes less space than a vector of those same ints - it doesn't have to store type information, or be ready to grow if you need it to, or... But the vector is getting transparently reused for you, while the array is getting copied every time. Obviously this example is intentionally poorly-written, but it would be easy to write something bad and discover that it's "better" than the right way. So I'm not sure what benefit all this would have. But it's pretty easy to do: (defmulti sizeof class) (defmethod sizeof Number ([_] 4)) ; or decide by size if you prefer (defmethod sizeof java.util.Collection ([coll] (reduce + 4 (map sizeof (seq coll))))) (defmethod sizeof clojure.lang.ISeq ([coll] (reduce + 4 (map sizeof (seq coll))))) (sizeof [1 2 [1 2]]) ;=> 24 Just add methods for more things you want to count. On Dec 22, 8:51 pm, Robert McIntyre <r...@mit.edu> wrote: > I think it would be really cool to have a function that gives the > total number of bytes that a data structure consumes. > > so something like: > > (total-memory [1 2 [1 2]]) > > would return however many bytes this structure is. > > Is there already something like this around? Would it be hard to write > to work for any clojure data structure? > > just like (time), I think (total-memory) would be very enlightening > for basic testing. > > sincerely, > > --Robert McIntyre -- 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