Loading...

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.

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 Expect to get a grade better than C. But C is actually a good grade, so many should be Writing a Technical Lab Report is like writing a job application! It should .

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 Sample lab write-up for computer science courses See this must read for detailed suggestions on how to write lab reports in computer science courses..

We chose an arbitrary length of 80characters for the string to be appended.