将数据值作为整数类型而不是字符串写入 CSV 有什么优势?

What are the advantages to writing data values to a CSV as an integer type rather than a string?

我正在合作一个项目,我目前已经构建了一个程序,可以将数据以字符串格式写入 CSV 文件。我的项目合作伙伴说他认为如果以整数格式编写产品会更有用,而我一直在争论我们的可视化功能在读取 CSV 数据时可以简单地 运行 一个 parseInt。

我想在这里问这个问题,以获取一些有关通过使用原始数据类型而不是字符串写入文件可以获得什么的信息。 Java 似乎真的是为了将 CSV 作为字符串写入 CSV 而构建的,但他声称将数据作为 int 写入会更有效率。想法?

这实际上更像是一个概念性问题,但我将包括我用来生成数据的代码 table 以防上下文很重要。

  //Snippet only
  private void elementLocator() {
        //Declare ArrayList to hold values
        data = new ArrayList<ArrayList<String>>();
        
        //Build data table
        try {
            //Unique xpath string
            String prefix = "//*[@id=\"main_table_countries_today\"]/tbody[1]/tr[";
            int j = 2;
            System.out.println("Retrieving data...");
            for(int i = 1; i <= 222; i ++) {
                try {
                    //Initialize array to fill in to data row by row
                    ArrayList<String> parser = new ArrayList<String>();
                    for(j = 2; j <= 13; j ++) {
                        parser.add(driver.findElement(By.xpath(prefix + i + "]/td[" + j + "]")).getText());
                    }
                    //Use a boolean indicator to skip any row that has a blank 1st column
                    String w = parser.get(0);
                    boolean v = w.isEmpty();
                    if(v) {
                        continue;
                    }
                    else {
                        data.add(parser);
                    }
                //Catch errors
                } catch (Exception e) {
                    e.printStackTrace();;
                    continue;
                }
        }
  }

  public void makeCSV() throws IOException {
        //Create output file only if it does not already exist
        EST est = new EST();
        //Pull year, month, day for file name
        String dt = est.getDate();
        f = new File(home + "\Climate Dev Pegasus\Data\Worldometer\" + dt + ".csv");
        if(!f.exists()) {
            try { 
                //Create FileWriter object with file as parameter 
                CSVWriter writer = new CSVWriter(new FileWriter(f, true));
                //Write headers
                String[] headers = "Country/Region, Total Cases, New Cases, Total Deaths, New Deaths, Total Recovered, Active Cases, Serious Cases, Tot Cases/1M pop, Deaths/1M pop, Total Tests, Tests/1M pop".split(",");
                writer.writeNext(headers);
                writer.flush();
                writer.close();
                //Give full modification permissions to file
                SetPermissions sp = new SetPermissions();
                sp.ChangePermissions(f);
            }catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        else {
        }
        path = Paths.get(home + "\Climate Dev Pegasus\Data\Worldometer\" + dt + ".csv");
        
        //Write data to file, allowing commas
        FileWriter csvWriter = new FileWriter(f,true);
        for(ArrayList<String> x : data) {
            for(String y : x) {
                String z = appendDQ(y);
                //int value = Integer.parseInt(z);
                csvWriter.append(z);
                csvWriter.append(",");
            }
            csvWriter.append("\n");
        }
        System.out.println("Data successfully written to file.");
        csvWriter.close();
}  

这是我在思考这个问题时想到的答案:

嗯,我认为这是一个非常基本的问题。

首先,最重要的一点。该程序应该易于其他开发人员理解,同时它应该足够强大,以至于最终用户在使用过程中不会受到干扰,例如加载时间过长。 但是要找到问题的答案,您应该走得更远。程序 运行 应该放在 PC 上还是嵌入式系统上? 由于 Java 默认情况下已经实现了字符串 class,因此它从头开始就非常强大。当然,整数总是更高效,因为它是原始数据类型!现在我假设给定的程序应该 运行 在 PC 或服务器上而不是在嵌入式系统上,因为对于这种情况,像 C 这样的语言会更合适 table。 我认为在这种情况下使用字符串实际上更有意义,因为 Java 的 comfortable 代码节省了开发时间并使代码对其他开发人员更具可读性。此外,字符串的使用可能导致需要额外的方法来将值转换为可以被后续程序读取的格式。但是,这会抵消任何性能优势。

最后但同样重要的是,此时可以参考一个有趣的例子。如果您以 CSV 格式导出 Excel table,那么也会在那里构建具有长字符串的文件。即使在那里,任何加载时间也不会打扰最终用户(在我看来)...