1 /* 2 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 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 package org.openjdk.bench.valhalla.sandbox.corelibs.corelibs.mapprotos; 24 25 import org.openjdk.jmh.annotations.Benchmark; 26 import org.openjdk.jmh.annotations.Setup; 27 import org.openjdk.jmh.annotations.State; 28 import org.openjdk.jmh.annotations.TearDown; 29 30 import java.util.HashMap; 31 import java.util.Map; 32 import java.util.function.IntFunction; 33 34 35 public class PutX extends MapBase { 36 IntFunction<Map<Integer, Integer>> mapSupplier; 37 Map<Integer,Integer> lastMap; 38 39 @Setup 40 public void setup() { 41 super.init(size); 42 try { 43 Class<?> mapClass = Class.forName(mapType); 44 mapSupplier = (size) -> newInstance(mapClass, size); 45 } catch (Exception ex) { 46 System.out.printf("%s: %s%n", mapType, ex.getMessage()); 47 return; 48 } 49 } 50 51 Map<Integer, Integer> newInstance(Class<?> mapClass, int size) { 52 try { 53 return (Map<Integer, Integer>)mapClass.getConstructor(int.class).newInstance(size); 54 } catch (Exception ex) { 55 throw new RuntimeException("failed", ex); 56 } 57 } 58 59 @TearDown 60 public void teardown() { 61 super.TearDown(lastMap); 62 } 63 64 @Benchmark 65 public Map<Integer, Integer> put() { 66 Integer[] keys = this.keys; 67 Map<Integer, Integer> map = mapSupplier.apply(0); 68 for (Integer k : keys) { 69 map.put(k, k); 70 } 71 lastMap = map; 72 return map; 73 } 74 75 @Benchmark 76 public Map<Integer, Integer> putSized() { 77 Integer[] keys = this.keys; 78 Map<Integer, Integer> map = mapSupplier.apply(size * 2); 79 for (Integer k : keys) { 80 map.put(k, k); 81 } 82 lastMap = map; 83 return map; 84 } 85 86 }