Having said that, I’m a bit surprised that the JIT doesn’t optimise this away. Have you tried using a benchmarking library like Criterium perhaps, which takes care of JIT warmup? You might find they come out the same.

]]>Thanks for providing these experiments. I was curious about type hinting, so I wrote two more functions and tested them:

(defn length3 [a b c]

(let [x ^double a

y ^double b

z ^double c]

(Math/sqrt (+ (* x x) (* y y) (* z z)))))

(defn length4 [^double x

^double y

^double z]

(Math/sqrt (+ (* x x) (* y y) (* z z))))

Here are my results:

length1 :

“Elapsed time: 210.436788 msecs”

length2 :

“Elapsed time: 27.204235 msecs”

length3 :

“Elapsed time: 36.905093 msecs”

length4 :

“Elapsed time: 18.779471 msecs”

So it is interesting to see that length4 seems to be the fastest of the four candidates (with type hints directly on the input parameters), whereas length3 that has type hints on the local variables inside the function is significantly slower than. Do you have any idea of what happens?

]]>