Let’s say that you want to profile a piece of code in clojurescript. There is a cool macro for that in clojurescript: time.

time evaluates an expression and prints the time it took and returns the value of the expression.

The interesting question is:

Where does time print the time it took?

Usually, it prints in the browser console. This is because, you have probably called (enable-console-print!) somewhere in your code.

But as a KLIPSE user, you want the time to be displayed in the evaluation rectangle instead of inside the console.

It’s simple to achieve that using with-out-str.

Let’s see it in action by comparing the running time of two naive implementations for prime number generation.

Here is a very naive implementation:

(defn is-prime? [n]
  (empty? (filter #(= 0 (mod n  %)) (range 2 n))))

(defn nth-prime [n]
  (last (take n (filter #(is-prime? %) (iterate inc 2)))))


(with-out-str (time (nth-prime 50)))

And now a less naive implementation:

(defn is-prime-opt? [n]
  (or (= 2 n)
     (not-any? #(= 0 (mod n %)) (range 3 (inc (Math/sqrt n)) 2))))

(defn nth-prime-opt [n]
  (last (take n (filter #(is-prime-opt? %) (cons 2 (iterate (partial + 2) 3))))))


(with-out-str (time (nth-prime-opt 50)))