Re: Passing primitives from an inner loop to an outer loop efficiently

2009-07-09 Thread Frantisek Sodomka
Jonathan showed destructuring/indexing very nicely. Here are some timings for vector/list creation: (let [lst '(1 2 3)] (timings 1e7 (list 1 2 3) (cons 1 (cons 2 (cons 3 ( (conj (conj (conj () 3) 2) 1) (vector 1 2 3) (vec lst) (conj (conj (conj [] 1) 2) 3))) 6590.

Re: Passing primitives from an inner loop to an outer loop efficiently

2009-07-08 Thread John Harrop
On Wed, Jul 8, 2009 at 4:57 PM, Frantisek Sodomka wrote: > > So far it seems that vectors win in Clojure: > > (timings 3e5 > (let [v (vector 1 2 3) a (nth v 0) b (nth v 1) c (nth v 2)] (+ a b > c)) > (let [lst (list 1 2 3) a (nth lst 0) b (nth lst 1) c (nth lst 2)] (+ > a b c))) > > => > 680.63

Re: Passing primitives from an inner loop to an outer loop efficiently

2009-07-08 Thread Jonathan Smith
Seperate so it can be easily ignored: Changed in core.clj: (defn nth "Returns the value at the index. get returns nil if index out of bounds, nth throws an exception unless not-found is supplied. nth also works for strings, Java arrays, regex Matchers and Lists, and, in O(n) time, for s

Re: Passing primitives from an inner loop to an outer loop efficiently

2009-07-08 Thread Jonathan Smith
On Jul 8, 4:57 pm, Frantisek Sodomka wrote: > So far it seems that vectors win in Clojure: > > (timings 3e5 >   (let [v (vector 1 2 3) a (nth v 0) b (nth v 1) c (nth v 2)] (+ a b > c)) >   (let [lst (list 1 2 3) a (nth lst 0) b (nth lst 1) c (nth lst 2)] (+ > a b c))) > > => >   680.63 ms   83.6%

Re: Passing primitives from an inner loop to an outer loop efficiently

2009-07-08 Thread Frantisek Sodomka
So far it seems that vectors win in Clojure: (timings 3e5 (let [v (vector 1 2 3) a (nth v 0) b (nth v 1) c (nth v 2)] (+ a b c)) (let [lst (list 1 2 3) a (nth lst 0) b (nth lst 1) c (nth lst 2)] (+ a b c))) => 680.63 ms 83.6% 1.2x (let [v (vector 1 2 3) a (nth v 0) b (nth v 1) c (nth

Re: Passing primitives from an inner loop to an outer loop efficiently

2009-07-08 Thread John Harrop
Interesting. How are these timings affected if you add in the time taken to pack the list or vector in the first place, though? I have the feeling it may be slightly cheaper to unpack a vector, but noticeably cheaper to pack a list... --~--~-~--~~~---~--~~ You recei

Re: Passing primitives from an inner loop to an outer loop efficiently

2009-07-08 Thread Frantisek Sodomka
To compare timings, I use this macro: (defmacro time-ms [n expr] `(let [start# (. System (nanoTime))] (dotimes [i# ~n] ~expr) (/ (double (- (. System (nanoTime)) start#)) 100.0))) (defmacro timings [n & expr] (let [expr-are-not-equal (cons 'not= expr) expr-times (co

Re: Passing primitives from an inner loop to an outer loop efficiently

2009-07-08 Thread John Harrop
On Wed, Jul 8, 2009 at 3:42 AM, Frantisek Sodomka 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) r

Re: Passing primitives from an inner loop to an outer loop efficiently

2009-07-08 Thread Frantisek Sodomka
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

Re: Passing primitives from an inner loop to an outer loop efficiently

2009-07-07 Thread Jonathan Smith
On Jul 7, 5:10 pm, John Harrop wrote: > Problem: Passing primitives from an inner loop to an outer loop efficiently. > Here is what I've found. > > The fastest method of result batching, amazingly, is to pass out a list and: > > (let [foo (loop ... ) > x (double (first foo)) > r1 (rest foo) > y