changeset 1128:1bb680b3d89a

Amend JMHSample_10_ConstantFold with the "final" case.
author shade
date Fri, 13 Feb 2015 01:18:19 +0300
parents ac61e7888e96
children 7c2b3d0880da
files jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_10_ConstantFold.java
diffstat 1 files changed, 14 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_10_ConstantFold.java	Fri Feb 13 00:54:22 2015 +0300
+++ b/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_10_ConstantFold.java	Fri Feb 13 01:18:19 2015 +0300
@@ -55,13 +55,17 @@
      * can cleverly optimize it. In our case, that means we can move the computation
      * outside of the internal JMH loop.
      *
-     * This can be prevented by always reading the inputs from the state, computing
-     * the result based on that state, and the follow the rules to prevent DCE.
+     * This can be prevented by always reading the inputs from the non-final instance fields
+     * of @State objects, computing the result based on those values, and the follow the
+     * rules to prevent DCE.
      */
 
     // IDEs will say "Oh, you can convert this field to local variable". Don't. Trust. Them.
     private double x = Math.PI;
 
+    // IDEs will probably also say "Look, it could be final". Don't. Trust. Them. Either.
+    private final double wrongX = Math.PI;
+
     @Benchmark
     public double baseline() {
         // simply return the value, this is a baseline
@@ -69,12 +73,18 @@
     }
 
     @Benchmark
-    public double measureWrong() {
+    public double measureWrong_1() {
         // This is wrong: the source is predictable, and computation is foldable.
         return Math.log(Math.PI);
     }
 
     @Benchmark
+    public double measureWrong_2() {
+        // This is wrong: the source is predictable, and computation is foldable.
+        return Math.log(wrongX);
+    }
+
+    @Benchmark
     public double measureRight() {
         // This is correct: the source is not predictable.
         return Math.log(x);
@@ -83,7 +93,7 @@
     /*
      * ============================== HOW TO RUN THIS TEST: ====================================
      *
-     * You can see the unrealistically fast calculation in with measureWrong(),
+     * You can see the unrealistically fast calculation in with measureWrong_*(),
      * while realistic measurement with measureRight().
      *
      * You can run this test: