< prev index next >

test/micro/org/openjdk/bench/jdk/incubator/foreign/TestLoadBytes.java

Print this page

  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 
 24 package org.openjdk.bench.jdk.incubator.foreign;
 25 
 26 import jdk.incubator.foreign.CLinker;
 27 import jdk.incubator.foreign.MemoryAccess;
 28 import jdk.incubator.foreign.MemoryAddress;
 29 import jdk.incubator.foreign.MemorySegment;
 30 import jdk.incubator.foreign.ResourceScope;
 31 import jdk.incubator.vector.ByteVector;
 32 import jdk.incubator.vector.IntVector;
 33 import jdk.incubator.vector.VectorSpecies;
 34 import org.openjdk.jmh.annotations.Benchmark;
 35 import org.openjdk.jmh.annotations.BenchmarkMode;
 36 import org.openjdk.jmh.annotations.CompilerControl;
 37 import org.openjdk.jmh.annotations.Fork;
 38 import org.openjdk.jmh.annotations.Measurement;
 39 import org.openjdk.jmh.annotations.Mode;
 40 import org.openjdk.jmh.annotations.OutputTimeUnit;
 41 import org.openjdk.jmh.annotations.Param;
 42 import org.openjdk.jmh.annotations.Setup;
 43 import org.openjdk.jmh.annotations.State;
 44 import org.openjdk.jmh.annotations.Warmup;
 45 
 46 import java.nio.ByteBuffer;
 47 import java.nio.ByteOrder;
 48 import java.util.concurrent.TimeUnit;
 49 


 50 @BenchmarkMode(Mode.AverageTime)
 51 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 52 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 53 @State(org.openjdk.jmh.annotations.Scope.Thread)
 54 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 55 @Fork(value = 1, jvmArgsAppend = {
 56         "--add-modules=jdk.incubator.foreign",
 57         "-Dforeign.restricted=permit",
 58         "--enable-native-access", "ALL-UNNAMED"})
 59 public class TestLoadBytes {
 60     @Param("1024")
 61     private int size;
 62 
 63     private byte[] srcArray;
 64     private ByteBuffer srcBufferNative;
 65     private MemorySegment srcSegmentImplicit;
 66 
 67     @Setup
 68     public void setup() {
 69         srcArray = new byte[size];

102             var v = srcBufferNative.get(i);
103             size += v;
104         }
105         return size;
106     }
107 
108     @Benchmark
109     public int bufferNativeScalarConst() {
110         int size = 0;
111         for (int i = 0; i < 1024; i++) {
112             var v = srcBufferNative.get(i);
113             size += v;
114         }
115         return size;
116     }
117 
118     @Benchmark
119     public int segmentNativeScalar() {
120         int size = 0;
121         for (int i = 0; i < srcArray.length; i++) {
122             var v = MemoryAccess.getByteAtOffset(srcSegmentImplicit, i);
123             size += v;
124         }
125         return size;
126     }
127 
128     @Benchmark
129     public int segmentNativeScalarConst() {
130         int size = 0;
131         for (int i = 0; i < 1024; i++) {
132             var v = MemoryAccess.getByteAtOffset(srcSegmentImplicit, i);
133             size += v;
134         }
135         return size;
136     }
137 }

  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 
 24 package org.openjdk.bench.jdk.incubator.foreign;
 25 



 26 import jdk.incubator.foreign.MemorySegment;
 27 import jdk.incubator.foreign.ResourceScope;



 28 import org.openjdk.jmh.annotations.Benchmark;
 29 import org.openjdk.jmh.annotations.BenchmarkMode;
 30 import org.openjdk.jmh.annotations.CompilerControl;
 31 import org.openjdk.jmh.annotations.Fork;
 32 import org.openjdk.jmh.annotations.Measurement;
 33 import org.openjdk.jmh.annotations.Mode;
 34 import org.openjdk.jmh.annotations.OutputTimeUnit;
 35 import org.openjdk.jmh.annotations.Param;
 36 import org.openjdk.jmh.annotations.Setup;
 37 import org.openjdk.jmh.annotations.State;
 38 import org.openjdk.jmh.annotations.Warmup;
 39 
 40 import java.nio.ByteBuffer;

 41 import java.util.concurrent.TimeUnit;
 42 
 43 import static jdk.incubator.foreign.ValueLayout.JAVA_BYTE;
 44 
 45 @BenchmarkMode(Mode.AverageTime)
 46 @Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 47 @Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
 48 @State(org.openjdk.jmh.annotations.Scope.Thread)
 49 @OutputTimeUnit(TimeUnit.NANOSECONDS)
 50 @Fork(value = 1, jvmArgsAppend = {
 51         "--add-modules=jdk.incubator.foreign",
 52         "-Dforeign.restricted=permit",
 53         "--enable-native-access", "ALL-UNNAMED"})
 54 public class TestLoadBytes {
 55     @Param("1024")
 56     private int size;
 57 
 58     private byte[] srcArray;
 59     private ByteBuffer srcBufferNative;
 60     private MemorySegment srcSegmentImplicit;
 61 
 62     @Setup
 63     public void setup() {
 64         srcArray = new byte[size];

 97             var v = srcBufferNative.get(i);
 98             size += v;
 99         }
100         return size;
101     }
102 
103     @Benchmark
104     public int bufferNativeScalarConst() {
105         int size = 0;
106         for (int i = 0; i < 1024; i++) {
107             var v = srcBufferNative.get(i);
108             size += v;
109         }
110         return size;
111     }
112 
113     @Benchmark
114     public int segmentNativeScalar() {
115         int size = 0;
116         for (int i = 0; i < srcArray.length; i++) {
117             var v = srcSegmentImplicit.get(JAVA_BYTE, i);
118             size += v;
119         }
120         return size;
121     }
122 
123     @Benchmark
124     public int segmentNativeScalarConst() {
125         int size = 0;
126         for (int i = 0; i < 1024; i++) {
127             var v = srcSegmentImplicit.get(JAVA_BYTE, i);
128             size += v;
129         }
130         return size;
131     }
132 }
< prev index next >