update-in for every function call might slow things down considerably.
Maybe using an atom referencing to native Java array for counts is
better (in which swap! would just use aset to new count)

On Jan 31, 5:45 pm, "Stephen C. Gilardi" <squee...@mac.com> wrote:
> On Jan 31, 2010, at 8:35 AM, Gabi wrote:
>
> > Is there any efficient way to get the number of times a given
> > function was executed (in run time not during profiling)? Maybe with
> > some clever use of its metadata ?
>
> Clojure function calls are low-level operations for efficiency. I'm not aware 
> of any easy hook for implementing that kind of profiling in Clojure. If 
> you're interested in a particular function, consider dedicating an atom to 
> holding a count of its calls and incrementing the value held by that atom on 
> each call:
>
>         user=> (def sqr-call-count (atom 0))
>         #'user/sqr-call-count
>         user=> (defn sqr [x] (swap! sqr-call-count inc) (* x x))
>         #'user/sqr
>         user=> (sqr 3)
>         9
>         user=> @sqr-call-count
>         1
>         user=> (def b (map sqr (range 100)))
>         #'user/b
>         user=> @sqr-call-count ; map is lazy
>         1
>         user=> b
>         (0 1 4 9 16 25 36 ... 9801)
>         user=> @sqr-call-count
>         101
>         user=>
>
> It's possible such a count could be kept in the function's metadata as you 
> suggest. If this kind of count were needed for a large number of functions, I 
> would stick with a single atom that held a map from function to count (or var 
> to count) that I updated with swap!, update-in, and inc.
>
> --Steve

-- 
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

Reply via email to