1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.openjdk.bench.vm.lambda.capture;
  24 
  25 import org.openjdk.jmh.annotations.Benchmark;
  26 import org.openjdk.jmh.annotations.BenchmarkMode;
  27 import org.openjdk.jmh.annotations.Mode;
  28 import org.openjdk.jmh.annotations.OutputTimeUnit;
  29 
  30 import java.util.Comparator;
  31 import java.util.concurrent.TimeUnit;
  32 
  33 /**
  34  * measure lambda capture
  35  *
  36  * @author Sergey Kuksenko (sergey.kuksenko@oracle.com)
  37  */
  38 @BenchmarkMode(Mode.AverageTime)
  39 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  40 public class Capture2 {
  41 
  42     public static class StaticInner implements Comparator<Integer> {
  43         @Override
  44         public int compare(Integer x, Integer y) {
  45             return (x < y) ? -1 : ((x > y) ? 1 : 0);
  46         }
  47     }
  48 
  49     public class InstanceInner implements Comparator<Integer> {
  50         @Override
  51         public int compare(Integer x, Integer y) {
  52             return (x < y) ? -1 : ((x > y) ? 1 : 0);
  53         }
  54     }
  55 
  56 
  57     @Benchmark()
  58     public Comparator<Integer> anonymous(){
  59         return new Comparator<Integer>() {
  60             @Override
  61             public int compare(Integer x, Integer y) {
  62                 return (x < y) ? -1 : ((x > y) ? 1 : 0);
  63             }
  64         };
  65     }
  66 
  67     @Benchmark()
  68     public Comparator<Integer> inner(){
  69         return new InstanceInner();
  70     }
  71 
  72     @Benchmark()
  73     public Comparator<Integer> inner_static(){
  74         return new StaticInner();
  75     }
  76 
  77     @Benchmark()
  78     public Comparator<Integer> lambda(){
  79         return (x, y) -> (x < y) ? -1 : ((x > y) ? 1 : 0);
  80     }
  81 
  82     public Comparator<Integer> mref_static(){
  83         return Integer::compare;
  84     }
  85 
  86     @Benchmark()
  87     public Comparator<Integer> mref_unbound(){
  88         return Integer::compareTo;
  89     }
  90 
  91     @Benchmark()
  92     public Comparator<Integer> mref_bound(){
  93         return this::compareInteger;
  94     }
  95 
  96     public int compareInteger(Integer x, Integer y) {
  97         return (x < y) ? -1 : ((x > y) ? 1 : 0);
  98     }
  99 
 100 
 101 }