简体   繁体   English

Java字符串拆分为数组,是否应该存储在变量中?

[英]Java string split into array, should I store in variable or not?

With our project we are going for MAXIMUM possible "speed". 在我们的项目中,我们力求最大可能的“速度”。 Would it be faster to split the string multiple times or to store it in a variable and use it? 将字符串多次拆分或将其存储在变量中并使用它会更快吗?

Example

    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]);

or 要么

    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]);

Which one would execute in less time? 哪一个执行时间更少?

In general, working with arrays is pretty fast and parsing Strings is not, so parsing the String only once and then working with the array will be significantly faster. 通常,使用数组的速度非常快,而解析Strings的速度不快,因此仅解析一次String,然后使用数组将明显更快。

Anyhow, this is easily measurable, just create a for with a few thousand elements and try both options to see the difference. 无论如何,这是很容易测量的,只需创建一个包含几千个元素的for,然后尝试两种选择以查看其区别。

Following is faster 跟随更快

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

Proof: 证明:

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

Putting Reece's answer to the test, I used this code 将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");

Executed this command: 执行此命令:

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

And got this output: 并得到以下输出:

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

This clearly indicates that the onesplit-approach ( String[] data=example.split(",") ) is much faster than the multisplit-approach ( example.split(",")[i] ), using 10,000 Strings. 这清楚地表明,使用10,000个String[] data=example.split(",") ,onesplit方法( String[] data=example.split(",") )比multisplit-approach( example.split(",")[i] )要快得多。

Sidenote: Using 100 Strings is the multisplit-approach 24ms and the onesplit-approach 6ms, 10 String multisplit 1ms and onesplit 0ms. 旁注:“使用100个字符串”是多重分割法24毫秒,“一个分割法” 6毫秒,“ 10字符串多重分割” 1毫秒和“一个零分割”。 I don't expect the multisplit-approach to be faster when using more numbers. 当使用更多数字时,我不希望采用多分割法。

Happy coding :) -Charlie 快乐的编码:)-查理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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