Faster way to clone.

Posted by AngryHacker on Stack Overflow See other posts from Stack Overflow or by AngryHacker
Published on 2010-05-06T22:33:35Z Indexed on 2010/05/06 22:38 UTC
Read the original article Hit count: 275

Filed under:
|
|
|
|

I am trying to optimize a piece of code that clones an object:

#region ICloneable
public object Clone()
{
    MemoryStream buffer = new MemoryStream();
    BinaryFormatter formatter = new BinaryFormatter();

    formatter.Serialize(buffer, this);     // takes 3.2 seconds
    buffer.Position = 0;
    return formatter.Deserialize(buffer);  // takes 2.1 seconds
}
#endregion

Pretty standard stuff. The problem is that the object is pretty beefy and it takes 5.4 seconds (according ANTS Profiler - I am sure there is the profiler overhead, but still).

Is there a better and faster way to clone?

© Stack Overflow or respective owner

Related posts about cloning

Related posts about c#