FileDocCategorySizeDatePackage
BufferingBadPerformance.javaAPI DocExample3355Sun Dec 14 22:47:30 GMT 2003oreilly.hcj.immutable

BufferingBadPerformance

public class BufferingBadPerformance extends Object
Demo Program for the "BufferingBadPerformance" Section.
author
Robert Simmons jr. (kraythe)
version
$Revision: 1.6 $

Fields Summary
private static final int
ARRAY_SIZE
Size of test array.
Constructors Summary
public BufferingBadPerformance()
Creates a new instance of BufferingBadPerformance


	       	 
	  
	
Methods Summary
public static final voiddumpArray(int[] array)
Dumps an array to a string using concatenations in append calls.

param
array The array to dump.

		final StringBuffer buf = new StringBuffer(500);
		buf.append("{");
		for (int idx = 0; idx < array.length; idx++) {
			buf.append("[" + idx + "] " + array[idx]);
		}
		buf.append("}");
	
public static final voiddumpArrayBetter(int[] array)
Dumps an array to a string without concatenations in append calls.

param
array The array to dump.

		final StringBuffer buf = new StringBuffer(500);
		buf.append("{");
		for (int idx = 0; idx < array.length; idx++) {
			buf.append("[");
			buf.append(idx);
			buf.append("] ");
			buf.append(array[idx]);
		}
		buf.append("}");
	
public static final voiddumpArrayReallyBad(int[] array)
Dumps an array to a string using allocation to perform concatenation.

param
array The array to dump.

		String result = new String("{");
		for (int idx = 0; idx < (array.length); idx++) {
			result += ("[" + idx + "] " + array[idx]);
		}
		result += "}";
	
public static voidmain(java.lang.String[] args)
Main demonstration method.

param
args command line arguments.

		final int[] array = new int[ARRAY_SIZE];
		int idx = 0;
		int total = 0;

		long millis = System.currentTimeMillis();

		for (idx = 0; idx < array.length; idx++) {
			total += (idx + 1);
			array[idx] = total;
		}

		System.out.println("Building " + array.length
		                   + " element Fibbonacci Number array took "
		                   + (System.currentTimeMillis() - millis) + " millis");

		dumpArray(array);
		System.out.println("Using dumpArray took "
		                   + (System.currentTimeMillis() - millis) + " millis");

		// *** Use the optimal dump method.
		millis = System.currentTimeMillis();
		dumpArrayBetter(array);
		System.out.println("Using dumpArrayBetter took "
		                   + (System.currentTimeMillis() - millis) + " millis");

		// *** Use dump method that does string allocations
		millis = System.currentTimeMillis();
		dumpArrayReallyBad(array);
		System.out.println("Using dumpArrayReallyBad took "
		                   + (System.currentTimeMillis() - millis) + " millis");

		millis = System.currentTimeMillis();