|I've written this code in two threads now (http://arclanguage.org/item?id=11129 and http://arclanguage.org/item?id=11104) to test memory efficiency by counting how many times cons is called.|
This gets tedious because it clobbers cons and you need to restart your Arc session to get it back. So, a quick & dirty utility is born.
arc> (= cons-count* 0)
arc> (let old-cons cons
(def cons (x y)
(old-cons x y)))
*** redefining cons
It'll bog down run times, but it might be useful. It's even the beginning of a really simple call-counting profiler, if someone wants to explore the idea. Examples:
(mac count-cons (expr)
(w/uniq (old-cons cons-count)
`(with (,old-cons cons ,cons-count 0)
(= cons (fn (x y) (++ ,cons-count) (,old-cons x y)))
(after (do1 ,expr
(prn "consed " (plural ,cons-count "time") "."))
(= cons ,old-cons)))))
Feel free to improve upon this. I don't know if there are any glaring issues like thread-safety or something because I just jammed this out.
arc> (count-cons (cons 1 2))
consed 1 time.
(1 . 2)
arc> (count-cons (map [apply + _] (pair '(1 3 5 7 9))))
consed 11 times.
(4 12 9)
arc> (count-cons (pair '(1 3 5 7 9) +))
consed 4 times.
(4 12 (9))