Database Reference
In-Depth Information
4.
Next, change mc-pi to call center-dist-hint and add a type hint to it:
(defn mc-pi-hint
(^double [n]
(let [in-circle (double
(->>
(repeatedly n rand-point)
(map center-dist-hint )
(filter #(<= % (double 1.0)))
count))]
(double
(* (double 4.0) (/ in-circle (double n)))))))
Finally, let's see if this helps:
user=> (bench (mc-pi-hint 100000))
Evaluation count : 1380 in 60 samples of 23 calls.
Execution time mean : 46.317966 ms
Execution time std-deviation : 1.237294 ms
Execution time lower quantile : 44.981042 ms ( 2.5%)
Execution time upper quantile : 49.716563 ms (97.5%)
Overhead used : 1.780493 ns
Found 5 outliers in 60 samples (8.3333 %)
low-severe 2 (3.3333 %)
low-mild 3 (5.0000 %)
Variance from outliers : 14.1789 % Variance is moderately inflated by
outliers
nil
In this case, it resulted in a slight slowdown. However, I have seen simple changes such as
this result in good performance improvements for a minimal amount of work.
How it works…
The irst type hint we used is the ^double , which indicates the functions' return types and
parameters. These allow the Clojure compiler to generate a bytecode that's well-optimized
for these return types and for working with the Java primitives.
The other use of type hints allow the Clojure compiler to resolve methods at compile time.
The clojure.string namespace has a good example of this:
user=> (require '[clojure.string :as string])
user=> (source string/trim)
(defn ^String trim
""Removes whitespace from both ends of string.""
 
Search WWH ::




Custom Search