Java 读取双精度矩阵的有效方法
这是一种非常快速的读取所有双精度矩阵的方法(此矩阵中没有NAs上缺失的元素)。大多数条目是非零双精度的,可能30%是零。尺寸大约为100万行和100列 下面是我正在使用的函数。但是,对于超过1G字节的矩阵,它的速度非常慢 我怎样才能做得更快?以下任何一项是否有帮助: -不要保存为csv并读取它,而是尝试保存为二进制格式或其他格式。 -在数据文件中转置矩阵,然后逐列读取,而不是像下面的函数那样逐行读取。 -以某种方式将矩阵序列化为Java对象以便重新读取Java 读取双精度矩阵的有效方法,java,matrix,io,performance,Java,Matrix,Io,Performance,这是一种非常快速的读取所有双精度矩阵的方法(此矩阵中没有NAs上缺失的元素)。大多数条目是非零双精度的,可能30%是零。尺寸大约为100万行和100列 下面是我正在使用的函数。但是,对于超过1G字节的矩阵,它的速度非常慢 我怎样才能做得更快?以下任何一项是否有帮助: -不要保存为csv并读取它,而是尝试保存为二进制格式或其他格式。 -在数据文件中转置矩阵,然后逐列读取,而不是像下面的函数那样逐行读取。 -以某种方式将矩阵序列化为Java对象以便重新读取 private static Vecto
private static Vector<Vector<Double>> readTXTFile(String csvFileName, int skipRows) throws IOException {
String line = null;
BufferedReader stream = null;
Vector<Vector<Double>> csvData = new Vector<Vector<Double>>();
try {
stream = new BufferedReader(new FileReader(csvFileName));
int count = 0;
while ((line = stream.readLine()) != null) {
count += 1;
if(count <= skipRows) {
continue;
}
String[] splitted = line.split(",");
Vector<Double> dataLine = new Vector<Double>(splitted.length);
for (String data : splitted) {
dataLine.add(Double.valueOf(data));
}
csvData.add(dataLine);
}
} finally {
if (stream != null)
stream.close();
}
return csvData;
}
private static Vector readTXTFile(字符串csvFileName,int skipRows)引发IOException{
字符串行=null;
BufferedReader流=null;
向量csvData=新向量();
试一试{
stream=newbufferedreader(newfilereader(csvFileName));
整数计数=0;
而((line=stream.readLine())!=null){
计数+=1;
if(count我更改了代码,以摆脱所有向量和双对象的创建,转而使用固定大小的矩阵(假设您知道或可以提前计算文件中的行数和列数)
我扔了500000行文件在它,并看到约25%的改善
private static double[][] readTXTFile(String csvFileName, int skipRows) throws IOException {
BufferedReader stream = null;
int totalRows = 500000, totalColumns = 6;
double[][] matrix = new double[totalRows][totalColumns];
try {
stream = new BufferedReader(new FileReader(csvFileName));
for (int currentRow = 0; currentRow < totalRows; currentRow++) {
String line = stream.readLine();
if (currentRow <= skipRows) {
continue;
}
String[] splitted = line.split(",");
for (int currentColumn = 0; currentColumn < totalColumns; currentColumn++) {
matrix[currentRow][currentColumn] = Double.parseDouble(splitted[currentColumn]);
}
}
} finally {
if (stream != null) {
stream.close();
}
}
return matrix;
}
private static double[]readTXTFile(字符串csvFileName,int skipRows)引发IOException{
BufferedReader流=null;
int totalRows=500000,totalColumns=6;
double[]]矩阵=新的double[totalRows][totalColumns];
试一试{
stream=newbufferedreader(newfilereader(csvFileName));
对于(int currentRow=0;currentRow