[英]How do I speed up the dataset reading process in java
我正在開發一個Java項目,因此我必須從excel表格中讀取數據集,以便稍后在項目中使用它們,因此我為讀取方法使用了一個單獨的類,它的工作原理不錯,但是需要很長時間(可能是10或11)分鍾)
public class readExcel {
String[] excelSheets = {"f1.xls","f2.xls","f3.xls","f4.xls","f5.xls","f6.xls","f7.xls","f8.xls","f9.xls"};
//Reading All The Excel Sheets
double[][][][] arraysSigmaMatrices=new double[9][30][13][13];
double[][][][] arrayDeterminantSigmaMatrices=new double[9][30][1][1];
double[][][][] arrayInverseSigmaMatrices=new double[9][30][13][13];
double[][][][] arraysSigmaDiagonalMatrices=new double[9][30][1][13];
double[][][] arraysMuMatrices=new double[9][30][13];
double[][][] arraysComponentProportionalMatrices=new double[9][1][30];
public void readExcelsheets() throws FileNotFoundException, IOException {
System.out.println("Wait to Read The Files...");
arraysSigmaMatrices();
arrayDeterminantSigmaMatrices();
arrayinverseSigmaMatrices();
arraysSigmaDiagonalMatrices();
arraysMuMatrices();
arraysComponentProportionalMatrices();
System.out.println("Done");
}
public void arraysSigmaMatrices() throws FileNotFoundException, IOException {
for(int catrgory = 0; catrgory < excelSheets.length; catrgory++) {
for(int ngauss = 0; ngauss < 30; ngauss++){
for(int row= 0; row < 13; row++) {
for(int column= 0; column < 13; column++) {
HSSFWorkbook workbook=new HSSFWorkbook(new FileInputStream(excelSheets[catrgory]));//to be able to create everything in the excel sheet
String sheetname="Sigma"+(String.valueOf(ngauss+1));//adding the index to the sheet name
HSSFSheet sheet=workbook.getSheet(sheetname);//getting the sheet
HSSFRow rows=sheet.getRow(row);
arraysSigmaMatrices[catrgory][ngauss][row][column]=rows.getCell(column).getNumericCellValue();
}
}
}
}
}
readExcel是用於在程序運行后獲取數據並將其保存在變量數組中以供以后使用的單獨類,“ arraysSigmaMatrices()”是獲取數據的方法之一。 我的問題是現在是否可以使流程更快得多?
我的第二個問題是,與java中運行線程的速度有什么關系?
當然,如果您看到代碼中的某些內容可以更好地完成,請隨時讓我知道謝謝
您應該像這樣更改方法。
public void arraysSigmaMatrices() throws FileNotFoundException, IOException {
for(int catrgory = 0; catrgory < excelSheets.length; catrgory++) {
try (FileInputStream input = new FileInputStream(excelSheets[catrgory])) {
HSSFWorkbook workbook=new HSSFWorkbook(input);//to be able to create everything in the excel sheet
}
for(int ngauss = 0; ngauss < 30; ngauss++){
String sheetname="Sigma"+(String.valueOf(ngauss+1));//adding the index to the sheet name
HSSFSheet sheet=workbook.getSheet(sheetname);//getting the sheet
for(int row= 0; row < 13; row++) {
HSSFRow rows=sheet.getRow(row);
for(int column= 0; column < 13; column++) {
arraysSigmaMatrices[catrgory][ngauss][row][column]=rows.getCell(column).getNumericCellValue();
}
}
}
}
}
這將減少文件讀取次數,並節省資源。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.