标签:public key entity 元素 基础上 执行时间 https 结果 identity
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class ForUpdate {
public static void main(String[] args) {
// for (int i = 0; i < 10000; i += 10) {
// loopGivenNum(i);
// }
for (int i = 10000; i < 100000; i += 10000) {
loopGivenNum(i);
}
System.out.println("----- done -----");
}
private static void loopGivenNum(int i) {
List<String> smallLoop = getLoopList(i);
List<String> bigLoop = getLoopList(2 * i);
long doByForTimes = doByFor(bigLoop, smallLoop);
long doByMapTimes = doByMap(bigLoop, smallLoop);
System.out.println("size " + i + ": " + doByForTimes + "," + doByMapTimes);
}
/**
* 获取循环变量
* @param size 循环变量元素个数
*/
private static List<String> getLoopList(int size) {
List<String> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
list.add(String.valueOf(i));
}
return list;
}
private static long doByFor(List<String> bigLoop, List<String> smallLoop) {
long startTime = System.currentTimeMillis();
for (String str1 : smallLoop) {
for (String str2 : bigLoop) {
if (str1.equals(str2)) {
continue;
}
}
}
return System.currentTimeMillis() - startTime;
}
/**
* 使用 Map 优化
* @param bigLoop
* @param smallLoop
*/
private static long doByMap(List<String> bigLoop, List<String> smallLoop) {
long startTime = System.currentTimeMillis();
// 转换成map
Map<String, String> loopMap = bigLoop.stream().collect(Collectors.toMap(k -> k, Function.identity()));
System.out.println(loopMap.size());
for (String str1 : smallLoop) {
if (loopMap.containsKey(str1)) {
continue;
}
}
return System.currentTimeMillis() - startTime;
}
}
输出结果:
size 10000: 756,97
size 20000: 3091,8
size 30000: 4342,7
size 40000: 8848,7
size 50000: 16317,7
size 60000: 31652,7
size 70000: 37078,7
标签:public key entity 元素 基础上 执行时间 https 结果 identity
原文地址:https://www.cnblogs.com/east7/p/11985671.html