如何加快java

时间:2018-04-14 11:12:51

标签: 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是一个独立的类,用于在程序运行后获取数据并将它们保存在变量数组中以便以后使用它们,#34; arraysSigmaMatrices()&#34;是获取数据的方法之一。 我现在的问题是如何让这个过程更快?

和我的第二个问题,在java中运行线程的速度是多少?

当然如果您在代码中看到某些内容可以更好地完成,请随时告诉我 感谢

1 个答案:

答案 0 :(得分:3)

你应该像这样改变你的方法。

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();
                }
            }
        }           
    }
}

这将减少文件读取次数,并节省资源。