I have a static method that when invoked 2 times in the same program, with
the exact same arguments, takes significantly different amounts of time to
complete.
The method takes an array of doubles and appends each number therein to a
StringBuffer along with some formatting. I've found that invoking this same
method 2 times with the exact same agrument that the second call completes in
roughly 1/5 the time as the first.
This behavior is most pronounced with array length < 1000, but even up to
100,000 the second call took less time than the first.
My question is whether this behavior is:
a) a StringBuffer problem solely, or
b) related to any intesive usage of String objects, or
c) a version specific JVM problem, and/or
d) a Mac JVM implementation problem, or
e) something else entirely.
thanks,
email@hidden
The source I've included that demonstrates this problem was originally
designed for another purpose which explains it's peculiar name, methods, and
comments.
MacOSX 10.2.8
java version "1.4.1_01"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-39)
Java HotSpot(TM) Client VM (build 1.4.1_01-14, mixed mode)
/**
* Test whether or not using a StringBuffer for concatenating Strings is more
* efficient than repeatedly calling System.out.println();
*/
public class StringBuffer_vs_Println {
private double[] n;
/*===== Constructor: ===== */
public StringBuffer_vs_Println(int len) {
n = new double[len];
for (int i=0; i<n.length; i++) n[i] = Math.random();
testPrintLN();
testPrintSB();
}
/* ===== Main: ===== */
public static void main(String[] args) {
if (args.length == 0) {
new StringBuffer_vs_Println(500);
} else if (args.length == 1) {
try {
int n = Integer.parseInt(args[0]);
new StringBuffer_vs_Println(n);
} catch (Exception e) {
System.out.println("Invalid argument: Must be integer");
}
}
}
// print w/ StringBuffer
private void testPrintSB() {
long now = System.currentTimeMillis();
//Printshop.printSB(n);
print(n);
long test2 = (System.currentTimeMillis() - now);
System.out.println("\nw/ StringBuffer\n" + "Time:\t" + test2);
}
// print w/ System.out.println()
private void testPrintLN() {
long now = System.currentTimeMillis();
//Printshop.print(n);
print(n);
long test1 = (System.currentTimeMillis() - now);
System.out.println("\nw/ Println\n" + "Time:\t" + test1);
}
// this method is adapted from Printshop.java which originally used
// System.out.println() instead of StringBuffer, but when I changed
// all methods to use StringBuffer and run the test again, I noticed
// the descrepancy.
public static void print(double[] n) {
StringBuffer sb = new StringBuffer();
sb.append("java.primitive.double" + "{" + "\n");
for (int i = 0; i<n.length; i++) {
sb.append("\t" + n[i] + "\n");
}
sb.append("}");
//System.out.println(sb.toString()); // not needed for comparison
sb = null; // I added this because I thought the JVM might be
// retaining sb from one call to next somehow
// and that would explain the faster execution.
// It didn't do anything about time differences
however,
// but doesn't hurt anything so just leave alone.
}
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden
This email sent to email@hidden