Path: blob/master/test/micro/org/openjdk/bench/java/util/HashMapToArray.java
41161 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.java.util;2324import org.openjdk.jmh.annotations.Benchmark;25import org.openjdk.jmh.annotations.BenchmarkMode;26import org.openjdk.jmh.annotations.Fork;27import org.openjdk.jmh.annotations.Measurement;28import org.openjdk.jmh.annotations.Mode;29import org.openjdk.jmh.annotations.OutputTimeUnit;30import org.openjdk.jmh.annotations.Param;31import org.openjdk.jmh.annotations.Scope;32import org.openjdk.jmh.annotations.Setup;33import org.openjdk.jmh.annotations.State;34import org.openjdk.jmh.annotations.Warmup;3536import java.util.Arrays;37import java.util.HashMap;38import java.util.LinkedHashMap;39import java.util.Map;40import java.util.concurrent.TimeUnit;4142@BenchmarkMode(Mode.AverageTime)43@OutputTimeUnit(TimeUnit.NANOSECONDS)44@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)45@Measurement(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)46@Fork(3)47@State(Scope.Thread)48public class HashMapToArray {4950@Param({"0", "1", "10", "1000", "100000"})51public int size;5253@Param({"HashMap", "LinkedHashMap"})54public String mapType;5556private Map<Integer, Integer> map;5758@Setup59public void setup() {60switch (mapType) {61case "HashMap":62map = new HashMap<>();63break;64case "LinkedHashMap":65map = new LinkedHashMap<>();66break;67default:68throw new IllegalStateException();69}70for (int i = 0; i < size; i++) {71map.put(i, i * i);72}73}7475@Benchmark76public Object[] testKeySetToArray() {77return map.keySet().toArray();78}7980@Benchmark81public Object[] testKeySetToArrayTyped() {82return map.keySet().toArray(new Integer[0]);83}8485@Benchmark86public Object[] testValuesToArray() {87return map.values().toArray();88}8990@Benchmark91public Object[] testValuesToArrayTyped() {92return map.values().toArray(new Integer[0]);93}94}959697