On 30/01/2012 13:16, Ulf Zibis wrote:
Isn't cloning faster than normal instantiation?
I can imagine, that behind the scenes cloning mainly only needs to
duplicate the binary footprint of an object.

I don't see a good reason why it should be (equally, I've not tried benchmarking).

For the immediate fields of an object, (partial) bitwise copying "by hand" should be of comparable performance to a bitwise clone. For copying the referenced objects, there is no benefit for the clone.

Implementation of HashMap.clone is:

        HashMap<K,V> result = null;
        try {
            result = (HashMap<K,V>)super.clone();
        } catch (CloneNotSupportedException e) {
            // assert false;
        }
        result.table = new Entry[table.length];
        result.entrySet = null;
        result.modCount = 0;
        result.size = 0;
        result.init();
        result.putAllForCreate(this);

        return result;

The "copy constructor" is slightly different in that it uses the default load factor (and minimum size) instead of copying.

        this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
                      DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
        putAllForCreate(m);

Tom

Reply via email to