标签:nano 结果 main for author int val init 创建
package com.exam; import java.util.HashMap; import java.util.Map; /** * @author zt on 2020/7/18 * <p> * 1、创建十个HashMap,每个HashMap含有10万条记录; * 2、传递不同的构造方法的参数,比较速度 * 3、实验参数:构造方法传参(16, 0.75f) 和 (16384, 0.75f) */ public class Demo1 { public static void main(String[] args) { Long sum = 0L; for (int i = 0; i < 10; i++) { // sum += new Demo1().inputMap1(16, 0.75f); sum += new Demo1().inputMap1(16384, 0.75f); } System.out.println(sum / 10); /** * 实验结果: * new Demo1().inputMap1(16, 0.75f); 耗时 14423460 * new Demo1().inputMap1(16384, 0.75f); 耗时 10528330 * * 可见,合理的设置Map的初始容量可以提高性能。 */ } public Long inputMap1(int initialCapacity, float loadFactor) { String key, value; Map<String, String> map1 = new HashMap<>(initialCapacity, loadFactor); Long start = System.nanoTime(); for (int i = 0; i < 100000; i++) { key = String.valueOf(i); value = "value"; } Long endTime = System.nanoTime(); Long time = endTime - start; System.out.println("time = " + time); return time; } }
标签:nano 结果 main for author int val init 创建
原文地址:https://www.cnblogs.com/tzzt01/p/13338038.html