【问题标题】:Java string split into array, should I store in variable or not?Java字符串拆分为数组,我应该存储在变量中吗?
【发布时间】:2014-11-18 01:54:39
【问题描述】:

在我们的项目中,我们将追求最大可能的“速度”。多次拆分字符串或将其存储在变量中并使用它会更快吗?

例子

    String example = "1,2,3,4";
    System.out.println(example.split(",")[0]);
    System.out.println(example.split(",")[1]);
    System.out.println(example.split(",")[3]);
    System.out.println(example.split(",")[4]);

    String[] example = "1,2,3,4".split(",");
    System.out.println(example[0]);
    System.out.println(example[1]);
    System.out.println(example[2]);
    System.out.println(example[3]);

哪一个会在更短的时间内执行?

【问题讨论】:

  • String[] example = "1,2,3,4".split(","); 如果您的字符串很长并且您需要使用所有子字符串。那么将其存储在数组中会很好,因为.split() 很耗时
  • 除了效率之外,在第二个示例中存储 String[] 还有另一个好处:如果拆分代码发生变化(例如,您想在 ", *" 上拆分),您只需要进行一项更改,而不是四项更改。这意味着您错过其中一项更改的可能性较小,这意味着出现错误的可能性较小。

标签: java arrays string optimization split


【解决方案1】:

一般来说,处理数组非常快,而解析字符串则不然,因此只解析一次字符串然后处理数组会明显更快。

无论如何,这很容易衡量,只需创建一个包含几千个元素的 for 并尝试这两个选项以查看差异。

【讨论】:

    【解决方案2】:

    测试 Reece 的答案,我使用了这段代码

        long startTime, endTime;
    
        int amountOfStrings=10000;
    
        System.err.println("Generating "+amountOfStrings+" String(s)");
        startTime=System.currentTimeMillis();
        String example="";
        for(int i=1; i<amountOfStrings-1; i++) {
            example+=i+",";
        }
        example+=amountOfStrings;
        endTime=System.currentTimeMillis();
        System.err.println("Done in "+(endTime-startTime)+"ms");
    
        System.err.println("Taking the multisplit-approach");
        startTime=System.currentTimeMillis();
        for(int i=0; i<amountOfStrings-1; i++) {
            System.out.println(example.split(",")[i]);
        }
        endTime=System.currentTimeMillis();
        System.err.println("Done in "+(endTime-startTime)+"ms");
    
        System.err.println("Taking the onesplit-approach");
        startTime=System.currentTimeMillis();
        String[] data=example.split(",");
        for(int i=0; i<amountOfStrings-1; i++) {
            System.out.println(data[i]);
        }
        endTime=System.currentTimeMillis();
        System.err.println("Done in "+(endTime-startTime)+"ms");
    

    执行了这个命令:

    $ java -jar ~/Desktop/Untitled.jar > ~/Desktop/Untitled.log
    

    得到了这个输出:

    Generating 10000 String(s)
    Done in 292ms
    Taking the multisplit-approach
    Done in 3906ms
    Taking the onesplit-approach
    Done in 275ms
    

    这清楚地表明 onesplit-approach (String[] data=example.split(",")) 比 multisplit-approach (example.split(",")[i]) 快得多,使用 10,000 个字符串。

    旁注:使用 100 个字符串是 multisplit-approach 24ms 和 onesplit-approach 6ms,10 String multisplit 1ms 和 onesplit 0ms。当使用更多数字时,我不希望多重分割方法更快。

    编码愉快 :) -Charlie

    【讨论】:

    • 感谢您的宝贵时间!这真的很有帮助:)
    【解决方案3】:

    跟随速度更快

    String[] example = "1,2,3,4".split(",");
    

    证明:

    Time taken to run first method 10000 times: 640 ms
    Time taken to run second method 10000 times: 531 ms
    
    Time taken to run first method 100000 times: 4144 ms
    Time taken to run second method 100000 times: 3763 ms
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-22
      • 2012-12-01
      • 2015-12-27
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多