我正在处理一个包含约60,000个点坐标的文本文件(我预计很快会扩大规模),并对每个点到其他每个点执行马氏距离,然后将结果输出为文本文件。这意味着我的结果将近3,600,000,000行长。我的程序每1或2秒创建约60,000行。
我认为我的代码无法进行多线程处理,这种算法有更好的编码方式吗?人们如何处理这些进程?
我认为我的代码无法进行多线程处理,这种算法有更好的编码方式吗?人们如何处理这些进程?
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Coord {
public int a,b,c,d,e,f;
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("/Users/evanlivingston/2a.txt", true)));
Scanner sc = new Scanner(new File("/Users/evanlivingston/1.txt"));
List<Coord> coords = new ArrayList<Coord>();{
// for each line in the file
while(sc.hasNextLine()) {
String[] numstrs = sc.nextLine().split("\\s+");
Coord c = new Coord();
c.a = Integer.parseInt(numstrs[1]);
c.b = Integer.parseInt(numstrs[2]);
c.c = Integer.parseInt(numstrs[3]);
c.d = Integer.parseInt(numstrs[4]);
c.e = Integer.parseInt(numstrs[5]);
c.f = Integer.parseInt(numstrs[6]);
coords.add(c);
}
// now you have all coords in memory
int counter = 0; {
for(int i=0; i<coords.size(); i++ )
for( int j=0; j<coords.size(); j++, counter++ )
{
Coord c1 = coords.get(i);
Coord c2 = coords.get(j);
double foo = ((c1.a - c2.a) * (c1.a - c2.a)) *1 ;
double goo = ((c1.b - c2.b) * (c1.b - c2.b)) *1 ;
double hoo = ((c1.c - c2.c) * (c1.c - c2.c)) *2 ;
double joo = ((c1.d - c2.d) * (c1.d - c2.d)) *2 ;
double koo = ((c1.e - c2.e) * (c1.e - c2.e)) *4 ;
double loo = ((c1.f - c2.f) * (c1.f - c2.f)) *4 ;
double zoo = Math.sqrt(foo + goo + hoo + joo + koo + loo);
out.println(counter + "; " + i + " " + j + " " + zoo);
System.out.println(counter + "; " + i + " " + j + " " + zoo);
}
out.flush();
out.close();
}
}
}
}
我的输入文件长这样
0 0 0 0 0 0 0
1 0 0 0 0 0 1
....
59318 12 2 12 2 12 2
第一个数字是占位符。这是一个列表,其中包含替换组合的所有可能性,但受限于最后一行中所见的数量。
现在看起来计算需要大约16个小时,这仍然太长了。更不用说我估计最终文本输出将达到约120 GB。