|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, Oracle America, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * * Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in the |
| 13 | + * documentation and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * * Neither the name of Oracle nor the names of its contributors may be used |
| 16 | + * to endorse or promote products derived from this software without |
| 17 | + * specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 23 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 29 | + * THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | +package org.openjdk.jmh.samples; |
| 32 | + |
| 33 | +import org.openjdk.jmh.annotations.*; |
| 34 | +import org.openjdk.jmh.runner.Runner; |
| 35 | +import org.openjdk.jmh.runner.RunnerException; |
| 36 | +import org.openjdk.jmh.runner.options.Options; |
| 37 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 38 | + |
| 39 | +import java.util.ArrayList; |
| 40 | +import java.util.Collections; |
| 41 | +import java.util.List; |
| 42 | +import java.util.Random; |
| 43 | +import java.util.concurrent.TimeUnit; |
| 44 | + |
| 45 | +@BenchmarkMode(Mode.AverageTime) |
| 46 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 47 | +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) |
| 48 | +@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) |
| 49 | +@Fork(5) |
| 50 | +@State(Scope.Benchmark) |
| 51 | +public class JMHSample_39_MemoryAccess { |
| 52 | + public static final int N = 20_000_000; |
| 53 | + |
| 54 | + /* |
| 55 | + * This example highlights the pitfall of accidentally measuring memory access instead of processing time. |
| 56 | + * |
| 57 | + * An int array has got a different memory layout than an ArrayList of boxed ints. |
| 58 | + * This can lead to useless results because the memory access is completely different. |
| 59 | + * Arrays save all their ints in one block on the heap while ArrayLists don't. |
| 60 | + * They save only references to the boxed ints in one block. |
| 61 | + * All the references point to the boxed ints which are usually spread all over the heap. |
| 62 | + * This leads to many cache misses with a big error: |
| 63 | + * |
| 64 | + * Benchmark Mode Cnt Score Error Units |
| 65 | + * JMHSample_39_MemoryAccess.sumArray avgt 25 4.887 ± 0.008 ms/op |
| 66 | + * JMHSample_39_MemoryAccess.sumArrayList avgt 25 35.765 ± 0.112 ms/op |
| 67 | + * JMHSample_39_MemoryAccess.sumArrayListShuffled avgt 25 169.301 ± 1.064 ms/op |
| 68 | + * |
| 69 | + * The Java Object Layout (JOL) is a tool with which the different memory layouts of arrays and ArrayLists can be |
| 70 | + * examined in more detail. |
| 71 | + */ |
| 72 | + |
| 73 | + private int[] intArray = new int[N]; |
| 74 | + private List<Integer> intList = new ArrayList<>(N); |
| 75 | + private List<Integer> shuffledIntList = new ArrayList<>(N); |
| 76 | + |
| 77 | + @Setup |
| 78 | + public void setup() { |
| 79 | + Random random = new Random(1234); |
| 80 | + for (int i = 0; i < N; i++) { |
| 81 | + intArray[i] = random.nextInt(); |
| 82 | + intList.add(intArray[i]); |
| 83 | + shuffledIntList.add(intArray[i]); |
| 84 | + } |
| 85 | + Collections.shuffle(shuffledIntList); |
| 86 | + } |
| 87 | + |
| 88 | + @Benchmark |
| 89 | + public long sumArray() { |
| 90 | + long sum = 0; |
| 91 | + for (int i = 0; i < N; i++) { |
| 92 | + sum += intArray[i]; |
| 93 | + } |
| 94 | + return sum; |
| 95 | + } |
| 96 | + |
| 97 | + @Benchmark |
| 98 | + public long sumArrayList() { |
| 99 | + long sum = 0; |
| 100 | + for (int i = 0; i < N; i++) { |
| 101 | + sum += intList.get(i); |
| 102 | + } |
| 103 | + return sum; |
| 104 | + } |
| 105 | + |
| 106 | + @Benchmark |
| 107 | + public long sumArrayListShuffled() { |
| 108 | + long sum = 0; |
| 109 | + for (int i = 0; i < N; i++) { |
| 110 | + sum += shuffledIntList.get(i); |
| 111 | + } |
| 112 | + return sum; |
| 113 | + } |
| 114 | + |
| 115 | + /* |
| 116 | + * ============================== HOW TO RUN THIS TEST: ==================================== |
| 117 | + * |
| 118 | + * You can run this test: |
| 119 | + * |
| 120 | + * a) Via the command line: |
| 121 | + * $ mvn clean install |
| 122 | + * $ java -jar target/benchmarks.jar JMHSample_39 |
| 123 | + * |
| 124 | + * b) Via the Java API: |
| 125 | + * (see the JMH homepage for possible caveats when running from IDE: |
| 126 | + * http://openjdk.java.net/projects/code-tools/jmh/) |
| 127 | + */ |
| 128 | + public static void main(String[] args) throws RunnerException { |
| 129 | + Options opt = new OptionsBuilder() |
| 130 | + .include(".*" + JMHSample_39_MemoryAccess.class.getSimpleName() + ".*") |
| 131 | + .build(); |
| 132 | + |
| 133 | + new Runner(opt).run(); |
| 134 | + } |
| 135 | +} |
0 commit comments