Hi,

On Tue, May 18, 2010 at 4:59 PM, braver <delivera...@gmail.com> wrote:

> On May 18, 10:47 am, "Heinz N. Gies" <he...@licenser.net> wrote:
> > Out of curiosity, can you remove the pmap and run them in the opposite
> order? as first (time (reps-sorted1? dreps)) then (time (reps-sorted2?
> dreps))?
>
> user=> (time (reps-sorted1? dreps))
> "Elapsed time: 8254.967 msecs"
> true
> (defn reps-sorted2? [dreps]
>   (->> dreps (map (fn [[_ days]]  ; NB pmap 3x slower here!
>     (let [s (map first days)]
>      (every? true? (map >= s (cons (first s) s))))))
>    (every? true?)))
> #'user/reps-sorted2?
> user=> (time (reps-sorted2? dreps))
> "Elapsed time: 15635.756 msecs"
> true
>
> Still twice as slower...
>


As David said pmap introduces some overhead you need to compensate by
batching your work.

On why the higher-order-function style is slower: it's mainly caused by
allocation, you have to reduce the amount of intermediaries seqs.
eg (every? true? (map f coll)) introduces a intermediate seq (map f coll)
which is uncalled for. (every? f coll) doesn't and is thus faster. Try:

(defn reps-sorted2? [dreps]
 (every? (fn [[_ days]]
           (let [s (map first days)]
             (every? true? (map >= s (cons (first s) s))))
   dreps))))

It should not be as fast as the iterative version because of your other
every? true?. Sadly here one can't refactor this call as above because
(every? f coll1 coll2) is not supported.
What you can do though is to define a sorted-by helper function to work
around this limitation:

(defn sorted-by? [pred s]
  (if-let [ns (next s)]
    (let [[a b] s]
      (and (pred a b) (recur pred ns)))
    true))

(sorted-by? is indeed iterative but every? implementation is iterative too
so I don't think I'm cheating here)

Then you can rewrite reps-sorted2? to:
(defn reps-sorted2? [dreps]
 (every? #(sorted-by? >= (map first (val %))) dreps))

and it should be as fast as the iterative version.

However, according to your sample data you seem to be checking if the keys
of a hashmap are sorted...

hth,

Christophe



-- 
Brussels, 23-25/6 http://conj-labs.eu/
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.cgrand.net/ (en)

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