繁体   English   中英

如何在Java中加快数据集读取过程

[英]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.

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