在我的自动化测试中,我无法访问XML或数据库。我想找到网格中特定列的重复记录。我的网格有20,000条记录。唯一的问题是我们无权访问任何数据库,因此如果我更改页面速度很慢,每个页面加载50条记录。存在20,000条记录的性能问题。
生成此结果后,您将对其进行缓存,因此不必在每次页面访问时都重新生成它。但是,在2毫秒内,您可能不会打扰。
这是一个定时的例子
static class MyRecord {
String text;
int id;
double d;
public MyRecord(String text, int id, double d) {
this.text = text;
this.id = id;
this.d = d;
}
public int getId() {
return id;
}
}
public static void main(String[] args) {
for (int t = 0; t < 100; t++) {
long start = System.nanoTime();
Random rand = new Random();
Map<Integer, MyRecord> map = IntStream.range(0, 20000)
.mapToObj(i -> new MyRecord("text-" + i, rand.nextInt(i+1), i))
.collect(Collectors.groupingBy(MyRecord::getId,
Collectors.reducing(null, (a, b) -> a == null ? b : a)));
long time = System.nanoTime() - start;
System.out.printf("Took %.1f ms to generate and collect duplicates%n", time/1e6);
}
}
此测试需要2.0毫秒才能生成并整理出重复的记录。您可以在Java 7中编写相同的代码,只是编写时间更长,但不会更慢。如果不必生成记录,它将更快。
为了进行比较,我将其与
Map<Integer, MyRecord> map = IntStream.range(0, 20000).parallel()
.mapToObj(i -> new MyRecord("text-" + i, rand.nextInt(i+1), i))
.collect(Collectors.groupingByConcurrent(MyRecord::getId,
Collectors.reducing(null, (a, b) -> a == null ? b : a)));
但现在需要16毫秒。:P
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句