Note that there are some explanatory texts on larger screens.

plurals
  1. POIs string concatenaion really that slow?
    primarykey
    data
    text
    <p>I'm currently looking into String concat options and the penalty they have on the overall performance. And my test-case creates results that blow my mind, I'm not sure if I'm overlooking something.</p> <p>Here is the deal: Doing <code>"something"+"somethingElse"</code> in java will (at compile-time) create a new <code>StringBuilder</code> every time this is done.</p> <p>For my test-case, I'm loading a file from my HDD that has <strong>1661 lines of example data</strong> (classic "Lorem Ipsum"). This question is <strong>not about the I/O performance</strong>, but about the performance of the different string concat methods.</p> <pre><code>public class InefficientStringConcat { public static void main(String[] agrs) throws Exception{ // Get a file with example data: System.out.println("Starting benchmark"); // Read an measure: for (int i = 0; i &lt; 10; i++){ BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(new File("data.txt"))) ); long start = System.currentTimeMillis(); // Un-comment method to test: //inefficientRead(in); //betterRead(in); long end = System.currentTimeMillis(); System.out.println("Took "+(end-start)+"ms"); in.close(); } } public static String betterRead(BufferedReader in) throws IOException{ StringBuilder b = new StringBuilder(); String line; while ((line = in.readLine()) != null){ b.append(line); } return b.toString(); } public static String inefficientRead(BufferedReader in) throws IOException { String everything = "", line; while ((line = in.readLine()) != null){ everything += line; } return everything; } } </code></pre> <p>As you can see, the setup is the same for both tests. Here are the results:</p> <p>Using <strong><code>inefficientRead()</code>-method</strong>:</p> <pre><code>Starting benchmark #1 Took 658ms #2 Took 590ms #3 Took 569ms #4 Took 567ms #5 Took 562ms #6 Took 570ms #7 Took 563ms #8 Took 568ms #9 Took 560ms #10 Took 568ms </code></pre> <p>Using <strong><code>betterRead()</code>-method</strong></p> <pre><code>Starting benchmark #1 Took 42ms #2 Took 10ms #3 Took 5ms #4 Took 7ms #5 Took 16ms #6 Took 3ms #7 Took 4ms #8 Took 5ms #9 Took 5ms #10 Took 13ms </code></pre> <p>I'm running the tests with <strong>no extra parameters</strong> to the <code>java</code>-command. I'm running a <a href="http://en.wikipedia.org/wiki/Mac_Mini#Specifications_2">MacMini3,1 from early 2009</a> and Sun JDK 7:</p> <pre><code>[luke@BlackBox ~]$ java -version java version "1.7.0_09" Java(TM) SE Runtime Environment (build 1.7.0_09-b05) Java HotSpot(TM) Client VM (build 23.5-b02, mixed mode) </code></pre> <p>This strikes me as a very heavy difference. Am I doing something wrong in measuring this, or is this supposed to happen?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload