Path: blob/master/test/micro/org/openjdk/bench/java/util/HashMapBench.java
41161 views
/*1* Copyright (c) 2018, Red Hat, Inc. 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*/2223package org.openjdk.bench.java.util;2425import org.openjdk.jmh.annotations.Benchmark;26import org.openjdk.jmh.annotations.BenchmarkMode;27import org.openjdk.jmh.annotations.Mode;28import org.openjdk.jmh.annotations.OutputTimeUnit;29import org.openjdk.jmh.annotations.Param;30import org.openjdk.jmh.annotations.Scope;31import org.openjdk.jmh.annotations.Setup;32import org.openjdk.jmh.annotations.State;3334import java.util.HashMap;35import java.util.LinkedHashMap;36import java.util.Map;37import java.util.concurrent.ThreadLocalRandom;38import java.util.concurrent.TimeUnit;39import java.util.function.Supplier;40import java.util.stream.IntStream;4142import static java.util.stream.Collectors.toMap;4344@BenchmarkMode(Mode.AverageTime)45@OutputTimeUnit(TimeUnit.MILLISECONDS)46@State(Scope.Thread)47public class HashMapBench {48private Supplier<Map<Integer, Integer>> mapSupplier;49private Map<Integer, Integer> bigMapToAdd;5051@Param("1000000")52private int size;5354@Param55private MapType mapType;5657public enum MapType {58HASH_MAP,59LINKED_HASH_MAP,60}6162@Setup63public void setup() {64switch (mapType) {65case HASH_MAP:66mapSupplier = () -> new HashMap<>();67break;68case LINKED_HASH_MAP:69mapSupplier = () -> new LinkedHashMap<>();70break;71default:72throw new AssertionError();73}7475ThreadLocalRandom rnd = ThreadLocalRandom.current();76this.bigMapToAdd = IntStream.range(0, size).boxed()77.collect(toMap(i -> 7 + i * 128, i -> rnd.nextInt()));78}7980@Benchmark81public int putAllWithBigMapToNonEmptyMap() {82Map<Integer, Integer> map = mapSupplier.get();83map.put(-1, -1);84map.putAll(bigMapToAdd);85return map.size();86}8788@Benchmark89public int putAllWithBigMapToEmptyMap() {90Map<Integer, Integer> map = mapSupplier.get();91map.putAll(bigMapToAdd);92return map.size();93}9495@Benchmark96public int put() {97Map<Integer, Integer> map = mapSupplier.get();98for (int k : bigMapToAdd.keySet()) {99map.put(k, bigMapToAdd.get(k));100}101return map.size();102}103}104105106