FileDocCategorySizeDatePackage
NewVsSetLen.javaAPI DocApache log4j 1.2.155885Sat Aug 25 00:09:36 BST 2007org.apache.log4j.performance

NewVsSetLen

public class NewVsSetLen extends Object
This program compares the cost of creating a new StringBuffer and converting it to a String versus keeping the same StringBuffer, setting its size to zero and then converting it to String.

The table below gives some figures.

Total Message length 0 1 2 4 8
New Buffer setLength New Buffer setLength New Buffer setLength New Buffer setLength New Buffer setLength
256 33 22 34 22 34 22 34 22 33 23
1024 58 41 59 45 59 48 59 51 60 44
4096 146 132 138 132 144 126 142 132 136 132
16384 617 593 593 609 601 617 601 632 593 632
65536 3218 3281 3093 3125 3125 3156 3125 3281 3062 3562
262144 14750 15125 14000 15500 14000 16125 14000 18000 14000 21375
1048576 87500 80000 60500 82000 57000 93000 57500 118500 57500 168500
Performance comparisons of new buffer creation versus setLength(0) approach for various message sizes and secondary loop lengths.

The tests copy a message to a destination string buffer and then copy a 256 character buffer to another buffer the number of times as specified by the secondary loop length.

The setLength(0) method is usually faster. However, after copying a large string it becomes slow even when copying small strings.

This is due to a peculiarity in the copy method in StringBuffer class which creates a character array of the same length as the old buffer even if the vast majority of those characters are unused.

The tests were performed on Linux using IBM's JDK 1.1.8.

The test script is a crude model of what might happen in reality. If you remain unconvinced of its results, then please send your alternative measurement scenario.

Fields Summary
static String
s
static int
BIGBUF_LEN
static int
SBUF_LEN
static int
RUN_LENGTH
static char[]
sbuf
static char[]
bigbuf
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)


  
    for(int i = 0; i < SBUF_LEN; i++) {
      sbuf[i] = (char) (i);
    }

    for(int i = 0; i < BIGBUF_LEN; i++) {
      bigbuf[i] = (char) (i);
    }
      
 
    int t;

    for(int len = SBUF_LEN; len <= BIGBUF_LEN; len*=4, RUN_LENGTH /= 4) {
      System.out.println("<td>"+len+"\n");
      for(int second = 0; second < 16;) {
	System.out.println("SECOND loop="+second +", RUN_LENGTH="
			   +RUN_LENGTH+", len="+len);
	t = (int)newBuffer(len, second);

	System.out.print("<td>" + t);
	t = (int)setLen(len, second);
	System.out.println(" <td>" + t + " \n");
	if(second == 0) {
	  second = 1;
	} else {
	  second *= 2;
	}
      }
    }
    
  
static doublenewBuffer(int size, int second)

    
    long before = System.currentTimeMillis();

    for(int i = 0; i < RUN_LENGTH; i++) {
      StringBuffer buf = new StringBuffer(SBUF_LEN);
      buf.append(sbuf, 0, sbuf.length);
      buf.append(bigbuf, 0, size);
      s = buf.toString();
    }

    for(int x = 0; x <  second; x++) {
      StringBuffer buf = new StringBuffer(SBUF_LEN);
      buf.append(sbuf, 0, SBUF_LEN);
      s = buf.toString();
    }
    return (System.currentTimeMillis() - before)*1000.0/RUN_LENGTH;    
  
static doublesetLen(int size, int second)

    long before = System.currentTimeMillis();

    StringBuffer buf = new StringBuffer(SBUF_LEN);

    for(int i = 0; i < RUN_LENGTH; i++) {
      buf.append(sbuf, 0, sbuf.length);
      buf.append(bigbuf, 0, size);
      s = buf.toString();
      buf.setLength(0);
    }

    for(int x = 0; x < second; x++) {
      buf.append(sbuf, 0, SBUF_LEN);
      s = buf.toString();
      buf.setLength(0);
    }
    return (System.currentTimeMillis() - before)*1000.0/RUN_LENGTH;