Methods Summary |
---|
public static final void | dumpArray(int[] array)Dumps an array to a string using concatenations in append calls.
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 void | dumpArrayBetter(int[] array)Dumps an array to a string without concatenations in append calls.
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 void | dumpArrayReallyBad(int[] array)Dumps an array to a string using allocation to perform concatenation.
String result = new String("{");
for (int idx = 0; idx < (array.length); idx++) {
result += ("[" + idx + "] " + array[idx]);
}
result += "}";
|
public static void | main(java.lang.String[] args)Main demonstration method.
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();
|