Reputation: 3861
All the examples say it is threadsafe and have examples like this:
Cloner cloner=new Cloner();
MyClass clone=cloner.deepClone(o);
In my code I am doing just that: Creating a new one each time I clone. So the questions is: Is this and expensive object that should created once and reused or is making a new one OK?
Upvotes: 2
Views: 1704
Reputation: 3861
Using this test:
@Test
public void loadTestCloner() {
Calendar cal = GregorianCalendar.getInstance();
long timer = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
Cloner c1 = new Cloner();
Calendar x = c1.deepClone(cal);
}
System.out.println("total time for new one each time: [" + (System.currentTimeMillis() - timer) + "]ms");
long timer2 = System.currentTimeMillis();
Cloner c2 = new Cloner();
for (int i = 0; i < 100000; i++) {
Calendar x = c2.deepClone(cal);
}
System.out.println("total time for reused one: [" + (System.currentTimeMillis() - timer2) + "]ms");
}
With Calander results:
And using our complex object:
And just using 1 object and using a "out of timer" Cloner:
So I conclude that, while not a lot of time, and since it is Threadsafe, it is good to make this once and reuse it.
Upvotes: 2