CPE 103-00Statement of ObjectiveIn his book Effective Java,Joshua Bloch warns against using the String concatenation operator forrepeated string concatentations. For once-only use like n("Result:"+total+"years"); result=result+"word"; Bloch recommends using Java's StringBuilder class as a much fasteralternative.
The purpose of this experiment is to see if String concatenation isreally as bad as Bloch says it is, and if so, to determine ifStringBuilder is really any better.
TheoryBloch explains that since String is immutable, it can't simply append"word" to result, but that it has to make a copy of both operands thencombine them and create a new string to hold the result Suggestions for clear lab reports in computer science courses (Your documentation should make clear what the code does at a high level, not how it does it at .
The copyoperation is very expensive; he asserts that it is a quadratic time operation. Soin theory we should expect to see the performance of StringBuilder ismuch faster than String.
Description of Experimental SetupThe experiments were performed on a desktop workstation with 3 GB RAMand a Dual Core Processor running at 2. ProcedureWe wrote a small program that implements both techniques. Onemethod, concat(), uses a loop to repeat a String concatentationoperation a given number of times.
A second method, concatFast()uses StringBuilder. We wrote a main method that calls them insidea loop that provides an increasing limit for the number ofconcatentations, so several experimental trials can be runautomatically.
The core of the concat() method builds a string using the concatenationoperator "+=". The concatFast() on the other hand, usesStringBuilder's append() method.
We chose an arbitrary length of 80characters for the string to be appended.