Java Reference

In-Depth Information

...

}

The architecture of the samples is designed to be loaded from a database, and that functional-

ity will be used in the examples in
Chapter 11
.
However, to facilitate running the examples,

most of the time they will use a mock entity manager that generates random data for the

series. In essence, most examples are module-level mesobenchmarks that are suitable for il-

lustrating the performance issues at hand—but we would only have an idea of the actual per-

formance of the application when the full application is run (as in
Chapter 11
)
.

One caveat is that a number of the examples are therefore dependent on the performance of

the random number generator in use. Unlike the microbenchmark example, this is by design,

as it allows the illustration of several performance issues in Java. (For that matter, the goal of

the examples is to measure the performance of some arbitrary thing, and the performance of

the random number generator fits that goal. That is quite different than a microbenchmark,

where including the time for generating random numbers would affect the overall calcula-

tion.)

The examples are also heavily dependent on the performance of the
BigDecimal
class,

which is used to store all the data points. This is a standard choice for storing currency data;

if the currency data is stored as primitive
double
objects, then rounding of half-pennies and

smaller amounts becomes quite problematic. From the perspective of writing examples, that

choice is also useful as it allows some “business logic” or lengthy calculation to oc-

cur—particularly in calculating the standard deviation of a series of prices. The standard de-

viation relies on knowing the square root of a
BigDecimal
number. The standard Java API

doesn't supply such a routine, but the examples use this method:

public

public static

static
BigDecimal
sqrtB
(
BigDecimal bd
) {

BigDecimal initial
=
bd
;

BigDecimal diff
;

ddo
{

BigDecimal sDivX
=
bd
.
divide
(
initial
,
8
,
RoundingMode
.
FLOOR
);

BigDecimal sum
=
sDivX
.
add
(
initial
);

BigDecimal div
=
sum
.
divide
(
TWO
,
8
,
RoundingMode
.
FLOOR
);

diff
=
div
.
subtract
(
initial
).
abs
();

diff
.
setScale
(
8
,
RoundingMode
.
FLOOR
);

initial
=
div
;

}
while

while
(
diff
.
compareTo
(
error
) >
0
);

return

return
initial
;

}