markthegrea
markthegrea

Reputation: 3861

Is creating a com.rits.cloning.Cloner expensive?

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

Answers (1)

markthegrea
markthegrea

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:

  • total time for new one each time: [644]ms
  • total time for reused one: [39]ms

And using our complex object:

  • total time for new one each time: [9585]ms
  • total time for reused one: [416]ms

And just using 1 object and using a "out of timer" Cloner:

  • total time for new one each time: [13]ms
  • total time for reused one: [0]ms

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

Related Questions