Log4j Version 1.2.14: Class NewVsSetLen

Log4j 1.2

Log4j 1.2.14

org.apache.log4j.performance Class NewVsSetLen

java.lang.Object
  |
  +--org.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
256
1024
4096
16384
65536
262144
1048576
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.


 
static void
 
 

NewVsSetLen

public NewVsSetLen()

main

public static void main(String[] args)

Log4j 1.2.14

Copyright 2000-2005 Apache Software Foundation.