On Tue, Dec 27, 2011 at 2:03 PM, endbegin <nkripl...@gmail.com> wrote:
> Hi,
>
> I am new to learning clojure, and I am hoping there is a solution to
> something that is not obvious to me ... I have a function that I want
> to run multiple times, measure the time it takes to execute each
> function, and put those numbers in a vector. I would like to process
> that vector further by doing things like max, min and average, and I
> think I can do that fairly "easily", but I'm having trouble generating
> such a vector in the first place.
>
> For instance when I do this:
>
> => (dorun (repeatedly 3 myfunc))
>
> I get
>
> "Elapsed time: 1253.764 msecs"
> "Elapsed time: 20003.997 msecs"
> "Elapsed time: 653.919 msecs"
> nil
>
> I would like to get something like
> [1253.764 200003.997 653.919], which I can post-process.
>
> I tried a
>
> => (conj [] (dorun (repeatedly 3 myfunc)))
>
> but that just gives me a [nil].
>
> Can anyone help?
>
> Thanks ...

The time macro *prints* the time rather than *returning* it. It
returns whatever the timed thing returned. For some purposes, such as
yours, that is unfortunate. You may want to use something like this:

(defmacro bench
  "Times the execution of forms, discarding their output and returning
a long in nanoseconds."
  ([& forms]
    `(let [start# (System/nanoTime)]
       ~@forms
       (- (System/nanoTime) start#))))

(untested) where you'd be able to gather results in a seq by methods like this:

user=> (repeatedly 3 #(bench (foo) (bar)))
(483458232 483458419 483457443)

HTH.

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