Database Reference
In-Depth Information
Using type hints
Most problems that are good targets for parallelization involve doing calculations in tight
loops. These places are good for all kinds of performance optimizations, from hoisting
conditionals out of them to ine-tuning compiler hints, which we will do here.
Being a dynamic language, Clojure doesn't require type declarations. However, if we know
what types we are using, we can get better performance by including type hints in our code.
This is helpful for object types, where Clojure can then resolve method calls at compile time,
and also for primitive types, where Clojure can generate well-tuned code that doesn't include
boxing or wrapping the primitive type in a heavier Java object.
For more information about type hints and working with Java primitives,
see the documentation on interacting with Java from Clojure at
http://clojure.org/java_interop .
Type hints are expressed as metadata tags for return types and object types. We'll see
examples of both in this recipe. For this recipe, we'll revisit the Monte Carlo simulation to
estimate the value of pi. We irst saw this in the Paritioning Monte Carlo simulations for better
pmap performance recipe. This example has enough low-level math which makes it a good
candidate for this kind of optimization.
Getting ready
We'll use the Criterium library for benchmarking, so include these in our Leiningen project.
clj ile:
(defproject parallel-data ""0.1.0""
:dependencies [[org.clojure/clojure ""1.6.0""]
[criterium ""0.4.3""]])
Use it and the java.lang.Math class in our script or REPL:
(use 'criterium.core)
(import [java.lang Math])
From the Partitioning Monte Carlo simulations for better pmap performance recipe, we'll use
the rand-point and center-dist functions.
 
Search WWH ::




Custom Search