// 当前系统毫秒 System.currentTimeMillis()// 计算用时
long time = System.currentTimeMillis(); ---代码 "耗时:" + (System.currentTimeMillis() - time)+"毫秒"
性能分析工具
JProfiler
代码
package com.song.list;
import java.util.HashMap;
import java.util.Map;
public class HashMapDemo {
public static void main(String[] args) {
//循环 1亿次
for (int num = 1; num <= 1000000000; num = num * 10) {
int capacity = (int) (num / 0.75 + 1);
String test = "test";
System.out.println("num is " + num);
Map<Integer, String> map = new HashMap<>();
Map<Integer, String> mapCapacity = new HashMap<>(capacity);
long strat1 = System.nanoTime();
for (int i = 0; i < num; i++) {
map.put(i, test);
}
long end1 = System.nanoTime();
System.out.println("没设置初始值的map: " + (end1 - strat1) + " capacity: " + "null");
long strat2 = System.nanoTime();
for (int i = 0; i < num; i++) {
mapCapacity.put(i, test);
}
long end2 = System.nanoTime();
System.out.println("设置了初始值的map: " + (end2 - strat2) + " capacity: " + capacity);
System.out.println("*****************************************************************");
}
}
}
日志输出: