code-reviews

borkdude 2015-12-08T09:19:22.000036Z

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

sveri 2015-12-08T12:32:39.000042Z

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

sveri 2015-12-08T12:32:47.000043Z

@borkdude:

borkdude 2015-12-08T12:51:55.000044Z

@sveri here you are: https://gist.github.com/borkdude/0efacfc4be56d98856f3

borkdude 2015-12-08T12:52:47.000046Z

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

borkdude 2015-12-08T12:53:07.000047Z

and I should use loops instead, to prevent boxing

sveri 2015-12-08T12:56:09.000048Z

@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

sveri 2015-12-08T13:13:35.000049Z

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

borkdude 2015-12-08T13:14:10.000050Z

maybe I should try aget-long?

sveri 2015-12-08T13:14:27.000051Z

does it work for you?

borkdude 2015-12-08T13:15:09.000052Z

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

sveri 2015-12-08T13:19:23.000053Z

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

sveri 2015-12-08T13:19:38.000054Z

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

borkdude 2015-12-08T14:55:17.000057Z

@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

borkdude 2015-12-08T14:58:01.000059Z

Fwiw I updated the gist too

roelof 2015-12-08T18:51:59.000064Z

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)