Created
August 19, 2020 15:55
-
-
Save karussell/8ab089dafc2f6e2fc6a2c43b57c7cfb4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Copyright (c) 2014, Oracle America, Inc. | |
| * All rights reserved. | |
| * | |
| * Redistribution and use in source and binary forms, with or without | |
| * modification, are permitted provided that the following conditions are met: | |
| * | |
| * * Redistributions of source code must retain the above copyright notice, | |
| * this list of conditions and the following disclaimer. | |
| * | |
| * * Redistributions in binary form must reproduce the above copyright | |
| * notice, this list of conditions and the following disclaimer in the | |
| * documentation and/or other materials provided with the distribution. | |
| * | |
| * * Neither the name of Oracle nor the names of its contributors may be used | |
| * to endorse or promote products derived from this software without | |
| * specific prior written permission. | |
| * | |
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | |
| * THE POSSIBILITY OF SUCH DAMAGE. | |
| */ | |
| package org.sample; | |
| import com.carrotsearch.hppc.IntIntHashMap; | |
| import com.graphhopper.storage.DataAccess; | |
| import com.graphhopper.storage.Directory; | |
| import com.graphhopper.storage.MMapDirectory; | |
| import com.graphhopper.storage.RAMDirectory; | |
| import org.openjdk.jmh.annotations.*; | |
| import java.util.Random; | |
| import static java.util.concurrent.TimeUnit.SECONDS; | |
| @State(Scope.Benchmark) | |
| @Warmup(iterations = 3, time = 5, timeUnit = SECONDS) | |
| @Measurement(iterations = 5, time = 5, timeUnit = SECONDS) | |
| @Fork(1) | |
| public class MyBenchmark { | |
| static int N = 200_000_000; | |
| @State(Scope.Thread) | |
| public static class DAState { | |
| Random r = new Random(0); | |
| DataAccess da; | |
| public DAState() { | |
| System.out.println("alloc da mem"); | |
| Directory ramDirectory = new RAMDirectory(); | |
| da = ramDirectory.find("da"); | |
| da.create(N * 4); | |
| for (int j = 0; j < N; j++) { | |
| da.setInt(j * 4, r.nextInt(N)); | |
| } | |
| } | |
| } | |
| @Benchmark | |
| public int testDA(DAState state) { | |
| return state.da.getInt(state.r.nextInt(N) * 4); | |
| } | |
| @State(Scope.Thread) | |
| public static class MDAState { | |
| DataAccess mda; | |
| Random r = new Random(0); | |
| public MDAState() { | |
| System.out.println("alloc mda mem"); | |
| Directory ramDirectory = new MMapDirectory("/tmp/test"); | |
| mda = ramDirectory.find("da"); | |
| mda.create(N * 4); | |
| for (int j = 0; j < N; j++) { | |
| mda.setInt(j * 4, r.nextInt(N)); | |
| } | |
| } | |
| } | |
| @Benchmark | |
| public int testMDA(MDAState state) { | |
| return state.mda.getInt(state.r.nextInt(N) * 4); | |
| } | |
| @State(Scope.Thread) | |
| public static class ArrayState { | |
| int[] array = new int[N]; | |
| Random r = new Random(0); | |
| public ArrayState() { | |
| System.out.println("alloc array mem"); | |
| for (int j = 0; j < N; j++) { | |
| array[j] = r.nextInt(N); | |
| } | |
| } | |
| } | |
| @Benchmark | |
| public int testArray(ArrayState state) { | |
| return state.array[state.r.nextInt(N)]; | |
| } | |
| @State(Scope.Thread) | |
| public static class MapState { | |
| IntIntHashMap map = new IntIntHashMap(N); | |
| Random r = new Random(0); | |
| public MapState() { | |
| System.out.println("alloc map mem"); | |
| for (int j = 0; j < N; j++) { | |
| int i = r.nextInt(N); | |
| map.put(j, i); | |
| } | |
| } | |
| } | |
| @Benchmark | |
| public int testMap(MapState state) { | |
| return state.map.get(state.r.nextInt(N)); | |
| } | |
| // @Benchmark | |
| // public int testNothing() { | |
| // return accessRand.nextInt(N); | |
| // } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment