Fork me on GitHub
#code-reviews
<
2015-12-08
>
borkdude09:12:22

I was wondering why this Clojure program takes ages to manipulate a mutable array, while a comparable Scala program only takes 2 seconds on my Macbook Air. Clojure: https://gist.github.com/borkdude/8bf5780efa371455e83d Scala: https://gist.github.com/borkdude/62094893df250225c9bc

sveri12:12:39

Hi, if I had time now I wonder if I would need the imput-day6.txt file to reproduce this?

borkdude12:12:47

@sveri: it has been discussed already in #C03RZGPG3 - seems there are two problems: aget and aset are slow, even with type hints

borkdude12:12:07

and I should use loops instead, to prevent boxing

sveri12:12:09

@borkdude: Yea, I followed it briefly, I am just curious if I see anything when I try it, maybe tonight when I am back home

sveri13:12:35

@borkdude: When I try to run your code I get this message after some time: IllegalArgumentException No matching method found: aget clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:80)

borkdude13:12:10

maybe I should try aget-long?

sveri13:12:27

does it work for you?

borkdude13:12:09

that doesn't exist. I should use a type hint then

sveri13:12:23

well, you might wan to make it work first...maybe with even a smaller array or something before you paste it

sveri13:12:38

No offense, I just wanted to try it, but if it's not working it's kind of meh

borkdude14:12:17

@sveri: that's very weird, the code should have worked. I wasn't aware of this problem. Anyway, I have three working examples now here. One with a transient vector and one with a mutable array. Similar performance, still not as great as in Scala: http://stackoverflow.com/questions/34153369/why-is-this-clojure-program-working-on-a-mutable-array-so-slow?noredirect=1#comment56064088_34153369

borkdude14:12:01

Fwiw I updated the gist too

roelof18:12:59

What do you experts think of this code:

def asym-hobbit-body-parts [{:name "head" :size 3}
                             {:name "left-eye" :size 1}
                             {:name "left-ear" :size 1}
                             {:name "mouth" :size 1}
                             {:name "nose" :size 1}
                             {:name "neck" :size 2}
                             {:name "left-shoulder" :size 3}
                             {:name "left-upper-arm" :size 3}
                             {:name "chest" :size 10}
                             {:name "back" :size 10}
                             {:name "left-forearm" :size 3}
                             {:name "abdomen" :size 6}
                             {:name "left-kidney" :size 1}
                             {:name "left-hand" :size 2}
                             {:name "left-knee" :size 2}
                             {:name "left-thigh" :size 4}
                             {:name "left-lower-leg" :size 3}
                             {:name "left-achilles" :size 1}
                             {:name "left-foot" :size 2}])


(defn matching-part
  [part replacement]
  {:name (clojure.string/replace (:name part) #"^left-" replacement)
   :size (:size part)})

(defn symmetrize-body-parts
  "Expects a seq of maps that have a :name and :size"
  [asym-body-parts]
  (reduce  (fn [final-body-parts part]
            (into final-body-parts (set [part (matching-part part "right1-")
                                         (matching-part part "right2-")
                                         (matching-part part "right3-")
                                         (matching-part part "right4-")
                                         (matching-part part "right5-")
                                 ])))
          []
          asym-body-parts))

(symmetrize-body-parts asym-hobbit-body-parts)