On Sun, Dec 7, 2008 at 2:22 AM, Randall R Schulz <[EMAIL PROTECTED]> wrote: > > On Saturday 06 December 2008 17:11, Christian Vest Hansen wrote: >> I played around with some code, trying to explore the memory problems >> with 'filter' that is discussed in another thread, when I noticed an >> unexpected behavior from 'nth'. >> >> Behold: >> >> user=> (nth (repeatedly (fn [] 0)) 10000000) >> java.lang.OutOfMemoryError: Java heap space (NO_SOURCE_FILE:0) >> user=> (defn xth [coll i] >> (if (zero? i) (first coll) (recur (rest coll) (dec i)))) >> #'user/xth >> user=> (xth (repeatedly (fn [] 0)) 10000000) >> 0 >> >> I could be wrong, but I don't think 'nth' should blow the heap here. > > I cannot reproduce this while invoking Java with the default heap > allocation.
Try adding another zero, then. Could be down to a difference between our JVMs. My point is that I think that 'nth' itself should be O(1) in memory usage (and I present 'xth' as an example of a similar function that is, though it's not a complete replacement for 'nth'), but it appears to me that 'nth's memory usage is dependent on the size of the collection it searches. > > Also: > > user=> (time (nth (repeatedly (fn [] 0)) 10000000)) > "Elapsed time: 7572.222 msecs" > 0 > user=> (time (nth (repeatedly (fn [] 0)) 10000000)) > "Elapsed time: 7632.135 msecs" > 0 > user=> (time (nth (repeatedly (fn [] 0)) 10000000)) > "Elapsed time: 7675.07 msecs" > 0 > user=> (time (nth (repeatedly (fn [] 0)) 10000000)) > "Elapsed time: 2236.852 msecs" > 0 > user=> (time (nth (repeatedly (fn [] 0)) 10000000)) > "Elapsed time: 2166.64 msecs" > 0 > user=> (time (nth (repeatedly (fn [] 0)) 10000000)) > "Elapsed time: 2270.884 msecs" > 0 > > > user=> (time (xth (repeatedly (fn [] 0)) 10000000)) > "Elapsed time: 3264.81 msecs" > 0 > user=> (time (xth (repeatedly (fn [] 0)) 10000000)) > "Elapsed time: 3257.63 msecs" > 0 > user=> (time (xth (repeatedly (fn [] 0)) 10000000)) > "Elapsed time: 3361.943 msecs" > 0 > user=> (time (xth (repeatedly (fn [] 0)) 10000000)) > "Elapsed time: 3245.313 msecs" > 0 > user=> (time (xth (repeatedly (fn [] 0)) 10000000)) > "Elapsed time: 3290.656 msecs" > 0 > user=> (time (xth (repeatedly (fn [] 0)) 10000000)) > "Elapsed time: 3264.101 msecs" > 0 > > > These results were for a newly launched instance. I submitted the > definition of (xth ...) between the two sequences of invocations shown > above. > > > Randall Schulz > > > > -- Venlig hilsen / Kind regards, Christian Vest Hansen. --~--~---------~--~----~------------~-------~--~----~ 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 To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---