Tuesday, May 31, 2005

Why clone is slower than newInstance

Ken Russell from the Java HotSpot VM Group, Sun Microsystems writes in java.net forums:
"Actually in the HotSpot JVM Object.clone() is not currently heavily optimized, while new instance is. You can feel free to file an RFE about this in the bug database. You can work around this problem by overriding clone() and manually allocate the new instance and assign the data into it. In fact, I think this is already probably necessary once your data structures get more complicated, which is why slow performance of Object.clone() hasn't shown up on our performance radar.
P.S. Here's a revised version of your test case which gets rid of startup transients:"


public class Main {
private boolean flag = true;
private String string = "Hello";
private int i = 0;

static class Child extends Main implements Cloneable {
private boolean flag2 = false;
private String string2 = "World";
private int i2 = -1;
}

private static int count = 1000000;

public static void testClone() throws CloneNotSupportedException {
Child child = new Child();
int res = 0;
long startTime = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
Child child2 = (Child) child.clone();
res += child2.i2;
}
long stopTime = System.currentTimeMillis();
System.out.println("" + count + " clones took " +
(stopTime - startTime) + " ms (dummy result = " + res + ")");
}

public static void testNewInstance() {
int res = 0;
long startTime = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
Child child = new Child();
res += child.i2;
}
long stopTime = System.currentTimeMillis();
System.out.println("" + count + " new operations took " +
(stopTime - startTime) + " ms (dummy result = " + res + ")");
}

public static void main(String[] args) throws CloneNotSupportedException {
testClone();
testClone();
testClone();
testNewInstance();
testNewInstance();
testNewInstance();
}
}

Friday, May 27, 2005

Slashdot: In 72 Hours, Your Ban Will Be Lifted

Has this happened to you?
"Your Headline Reader Has Been Banned".Your RSS reader is abusing the Slashdot server. You are requesting pages more often than our terms of service allow. "Do Not Bother Contacting Us For 72 Hours".Very funny.
The faq page says this

Wednesday, May 25, 2005

Cartoon blogging

This is simply fantastic. This guy's got talent.

Friday, May 20, 2005

Thursday, May 19, 2005

Netbeans mobility pack ROCKS!!!!!

Sat through the Sun tech days session on writing MIDP apps with netBeans. Chuk-Munn Lee simply rocks!! Now, the NB mobility pack is an amazing piece of software -beautifully crafted and shockingly productive. It made me want to give up J2SE dev & take up J2ME full time. :p

More on the J2ME capabilities of NB here and here.