简体   繁体   中英

How do I speed up the dataset reading process in java

I am working on a java project and I have to read the dataset from excel sheets to use them later in the project, so I have a separated class for the reading methods, its working well but it takes a long time (maybe 10 or 11 mins)

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 is the separated class for getting the data once the program runs and save them in the variable arrays to use them later,"arraysSigmaMatrices()"is one of the methods to get the data. my question now is there anyway to make the process much faster?

and my second question,what is the speed of running threads in java related to?

and of course if you see something in the code can be done in a better way feel free to let me know thanks

You should change your method like this.

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

This will reduce the number of file reads, and also save resources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM