Boolean valueOf instead of new Boolean

Otávio Gonçalves de Santana otaviojava at java.net
Sat May 24 18:52:58 UTC 2014


Hi Alexis.
Thank you for fixes the benchMarck.

About the class GSSManagerImpl, yes it is necessary because the
System.getProperty(USE_NATIVE_PROP) returns a String.


On Sat, May 24, 2014 at 12:19 PM, Aleksey Shipilev <
aleksey.shipilev at oracle.com> wrote:

> On 05/24/2014 06:34 PM, Otávio Gonçalves de Santana wrote:
> > The Boolean class has cache for true and false and using it, will save
> > memory and will faster than using create new instance of boolean.
> > Using JMH[1] with a code test[2] the result was:
>
> I agree Boolean.valueOf (whether explicit or implicit) should be used if
> identity is not required. Do you really need explicit Boolean.valueOf
> in, say, GSSManagerImpl, instead of relying on autoboxing?
>
> That said, your benchmark is not correct. At very least, you have to use
> explicit BlackHoles to avoid DCE [1][2]. This is how you do it:
>
> @State(Scope.Thread)
> @OutputTimeUnit(TimeUnit.SECONDS)
> public class BooleanBenchmark {
>
>     @Param("1000000")
>     private int size;
>
>     private List<String> booleanString;
>     private boolean[] booleans;
>
>     @Setup
>     public void s() {
>         booleans = new boolean[size];
>         booleanString = new ArrayList<String>(size);
>
>         for (int index = 0; index < size; index++) {
>             if (index % 2 == 0) {
>                 booleans[index] = true;
>                 booleanString.add(Boolean.TRUE.toString());
>             } else {
>                 booleans[index] = false;
>                 booleanString.add(Boolean.FALSE.toString());
>             }
>         }
>     }
>
>     @GenerateMicroBenchmark
>     public void valueOfBoolean(BlackHole bh) {
>         for (boolean b : booleans) {
>             Boolean result = b;
>             bh.consume(result);
>         }
>     }
>
>     @GenerateMicroBenchmark
>     public void valueOfString(BlackHole bh) {
>         for (String b : booleanString) {
>             Boolean result = Boolean.valueOf(b);
>             bh.consume(result);
>         }
>     }
>
>     @GenerateMicroBenchmark
>     public void newInstanceBoolean(BlackHole bh) {
>         for (boolean b : booleans) {
>             Boolean result = new Boolean(b);
>             bh.consume(result);
>         }
>     }
>
>     @GenerateMicroBenchmark
>     public void newInstanceString(BlackHole bh) {
>         for (String b : booleanString) {
>             Boolean result = new Boolean(b);
>             bh.consume(result);
>         }
>     }
> }
>
> Thanks,
> -Aleksey.
>
>
> [1]
>
> http://hg.openjdk.java.net/code-tools/jmh/file/75f8b23444f6/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_08_DeadCode.java
> [2]
>
> http://hg.openjdk.java.net/code-tools/jmh/file/75f8b23444f6/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_09_Blackholes.java
>
>


-- 
Atenciosamente.

Otávio Gonçalves de Santana

blog:     http://otaviosantana.blogspot.com.br/
twitter: http://twitter.com/otaviojava
site:     http://www.otaviojava.com.br
(11)     98255-3513



More information about the core-libs-dev mailing list