我的问题的目标是通过在大型数组列表上划分循环迭代的范围来提高算法的性能。
例如:我有一个数组列表,其大小约为100亿个长值条目,我试图实现的目标是从0到1亿个条目开始循环,无论计算如何都输出1亿个条目的结果在循环内;然后开始,将1亿到2亿做上一个并输出结果,然后是300-400百万,400-5亿,依此类推。
在获得所有1000亿/ 100万个结果之后,我可以将其汇总到循环外部,从并行的循环输出中收集结果。
我尝试使用一个可以通过尝试使用动态范围转换方法来实现相似效果的范围,但是我似乎无法像我希望的那样完全实现逻辑。
public static void tt4() {
long essir2 = 0;
long essir3 = 0;
List cc = new ArrayList<>();
List<Long> range = new ArrayList<>();
// break point is a method that returns list values, it was converted to
// string because of some concatenations and would be converted back to long here
for (String ari1 : Breakpoint()) {
cc.add(Long.valueOf(ari1));
}
// the size of the List is huge about 1 trillion entries at the minimum
long hy = cc.size() - 1;
for (long k = 0; k < hy; k++) {
long t1 = (long) cc.get((int) k);
long t2 = (long) cc.get((int) (k + 1));
// My main question: I am trying to iterate the entire list in a dynamic way
// which would exclude repeated endpoints on each iteration.
range = LongStream.rangeClosed(t1 + 1, t2)
.boxed()
.collect(Collectors.toList());
for (long i : range) {
// Hard is another method call on the iteration
// complexcalc is a method as well
essir2 = complexcalc((int) i, (int) Hard(i));
essir3 += essir2;
}
}
System.out.println("\n" + essir3);
}
我没有任何错误,我只是在寻找一种提高性能和时间的方法。我可以在一秒钟内直接完成一百万个条目,但是当我输入所需的大小时,它将永远运行。我给出的大小是用来说明大小大小的摘要,我不希望1000亿的意见不多,如果我在一秒钟内可以做到一百万,我在说的是大量的数字,我需要反复进行复杂的任务和调用,如果需要的话,我只需要帮助我尝试实现的逻辑即可。
答案 0 :(得分:0)
我建议立即采取的一件事是将您的Breakpoint
返回值存储在一个简单数组中,而不是使用List
。这样可以大大缩短执行时间:
List<Long> cc = new ArrayList<>();
for (String ari1 : Breakpoint()) {
cc.add(Long.valueOf(ari1));
}
Long[] ccArray = cc.toArray(new Long[0]);
我相信您正在寻找的是将您的任务分配到多个线程中。您可以使用ExecutorService “这简化了异步模式下任务的执行”。。
请注意,我不太熟悉整个概念,但是最近进行了一些试验,为您提供了如何实现此概念的快速草稿。
我欢迎那些具有多线程经验的人更正此帖子或在评论中提供其他信息以帮助改善此答案。
可运行任务类
public class CompartmentalizationTask implements Runnable {
private final ArrayList<Long> cc;
private final long index;
public CompartmentalizationTask(ArrayList<Long> list, long index) {
this.cc = list;
this.index = index;
}
@Override
public void run() {
Main.compartmentalize(cc, index);
}
}
主班
private static ExecutorService exeService = Executors.newCachedThreadPool();
private static List<Future> futureTasks = new ArrayList<>();
public static void tt4() throws ExecutionException, InterruptedException
{
long essir2 = 0;
long essir3 = 0;
ArrayList<Long> cc = new ArrayList<>();
List<Long> range = new ArrayList<>();
// break point is a method that returns list values, it was converted to
// string because of some concatenations and would be converted back to long here
for (String ari1 : Breakpoint()) {
cc.add(Long.valueOf(ari1));
}
// the size of the List is huge about 1 trillion entries at the minimum
long hy = cc.size() - 1;
for (long k = 0; k < hy; k++) {
futureTasks.add(Main.exeService.submit(new CompartmentalizationTask(cc, k)));
}
for (int i = 0; i < futureTasks.size(); i++) {
futureTasks.get(i).get();
}
exeService.shutdown();
}
public static void compartmentalize(ArrayList<Long> cc, long index)
{
long t1 = (long) cc.get((int) index);
long t2 = (long) cc.get((int) (index + 1));
// My main question: I am trying to iterate the entire list in a dynamic way
// which would exclude repeated endpoints on each iteration.
range = LongStream.rangeClosed(t1 + 1, t2)
.boxed()
.collect(Collectors.toList());
for (long i : range) {
// Hard is another method call on the iteration
// complexcalc is a method as well
essir2 = complexcalc((int) i, (int) Hard(i));
essir3 += essir2;
}
}