To compare timings, I use this macro:
(defmacro time-ms [n expr]
`(let [start# (. System (nanoTime))]
(dotimes [i# ~n]
~expr)
(/ (double (- (. System (nanoTime)) start#)) 1000000.0)))
(defmacro timings [n & expr]
(let [expr-are-not-equal (cons 'not= expr)
expr-times (cons 'vector (map #(list 'time-ms n %) expr))]
`(if ~expr-are-not-equal
(do (println "Expressions:\n")
(dorun (map prn '~expr))
(println "\nare NOT equal!"))
(let [ts# ~expr-times
max-time# (apply max ts#)]
(dorun (map (fn [~'t ~'e] (printf "%8.2f ms %6.1f%% %5.1fx "
~'t (* 100.0 (/ ~'t max-time#)) (/ max-time# ~'t))
(prn ~'e))
ts# '~expr))))))
Use is:
user=> (timings 1e6 (+ 2 4 5) (+ 2 (+ 4 5)))
727.50 ms 100.0% 1.0x (+ 2 4 5)
353.18 ms 48.5% 2.1x (+ 2 (+ 4 5))
For our case:
(let [v [1 2 3] lst '(1 2 3)]
(timings 1e5
(let [[a b c] v] [a b c])
(let [a (v 0) b (v 1) c (v 2)] [a b c])
(let [a (nth v 0) b (nth v 1) c (nth v 2)] [a b c])
(let [a (first v) b (second v) c (first (rest (rest v)))] [a b c])
(let [x (first v) r1 (rest v) y (first r1) r2 (rest r1) z (first
r2)] [x y z])
(let [[a b c] lst] [a b c])
;; (let [a (lst 0) b (lst 1) c (lst 2)] [a b c])
(let [a (nth lst 0) b (nth lst 1) c (nth lst 2)] [a b c])
(let [a (first lst) b (second lst) c (first (rest (rest lst)))] [a
b c])
(let [x (first lst) r1 (rest lst) y (first r1) r2 (rest r1) z
(first r2)] [x y z])))
And on my computer I get these numbers:
145.86 ms 64.5% 1.6x (let [[a b c] v] [a b c])
97.37 ms 43.1% 2.3x (let [a (v 0) b (v 1) c (v 2)] [a b c])
94.66 ms 41.9% 2.4x (let [a (nth v 0) b (nth v 1) c (nth v 2)]
[a b c])
226.15 ms 100.0% 1.0x (let [a (first v) b (second v) c (first
(rest (rest v)))] [a b c])
205.05 ms 90.7% 1.1x (let [x (first v) r1 (rest v) y (first r1)
r2 (rest r1) z (first r2)] [x y z])
219.42 ms 97.0% 1.0x (let [[a b c] lst] [a b c])
171.32 ms 75.8% 1.3x (let [a (nth lst 0) b (nth lst 1) c (nth
lst 2)] [a b c])
179.03 ms 79.2% 1.3x (let [a (first lst) b (second lst) c
(first (rest (rest lst)))] [a b c])
170.41 ms 75.4% 1.3x (let [x (first lst) r1 (rest lst) y (first
r1) r2 (rest r1) z (first r2)] [x y z])
Frantisek
On Jul 8, 9:53 am, John Harrop <[email protected]> wrote:
> On Wed, Jul 8, 2009 at 3:42 AM, Frantisek Sodomka <[email protected]>wrote:
>
>
>
> > If result is a vector v, then from these 4 cases:
> > (let [v [1 2 3]]
> > (let [[a b c] v] a b c)
> > (let [a (v 0) b (v 1) c (v 2)] a b c)
> > (let [a (nth v 0) b (nth v 1) c (nth v 2)] a b c)
> > (let [x (first v) r1 (rest v) y (first r1) r2 (rest r1) z (first
> > r2)] x y z))
>
> > using 'nth'
> > (let [a (nth v 0) b (nth v 1) c (nth v 2)] a b c)
> > is the fastest.
>
> Is it faster than using list and manually destructuring the list?
>
> If so, that would mean that (nth v n) is faster than (get v n), which would
> be odd since the latter should turn into the former anyway and be inlinable
> by the JIT.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
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
-~----------~----~----~----~------~----~------~--~---