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
Hi, if I had time now I wonder if I would need the imput-day6.txt file to reproduce this?
@sveri here you are: https://gist.github.com/borkdude/0efacfc4be56d98856f3
@sveri: it has been discussed already in #C03RZGPG3 - seems there are two problems: aget and aset are slow, even with type hints
and I should use loops instead, to prevent boxing
@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
@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)
maybe I should try aget-long?
does it work for you?
that doesn't exist. I should use a type hint then
well, you might wan to make it work first...maybe with even a smaller array or something before you paste it
No offense, I just wanted to try it, but if it's not working it's kind of meh
@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
Fwiw I updated the gist too
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)