1

Following this answer, I successfully copied my object.

However, performance is the highest priority in my current project. So I did some unit testing and checked how long it took to execute. Result was an average of 2sec while I was expecting max 200ms! I found out that 90% of the time was spend in the Object copier.

Is there a way to increase this performance? Any other way to copy an object? Does anyone knows how fast copying an array or list goes? I would consider using such.

Community
  • 1
  • 1
dylanmensaert
  • 1,689
  • 5
  • 24
  • 39
  • how many objects require this facility? – qujck Mar 18 '13 at 21:10
  • 1, but it is copied several times. The most important property in my object is a multidimensional array. So I could change that (with lots of modification :p ) into a simple array or list. I'm willing to do this if it increases the performance greatly – dylanmensaert Mar 18 '13 at 21:12
  • a hard coded copy will outperform anything else – qujck Mar 18 '13 at 21:12
  • Without any information about what kind of object you're copying, or the context in which it is copied, this question cannot be answered. And, really, if "performance is the highest priority," you would be writing in assembly language. – Jim Mischel Mar 18 '13 at 21:15
  • 1
    @JimMischel He specifically stated that his code is taking 2 seconds but his functional requirements dictate 200ms. He doesn't need "the best possible performance" he just needs to get to 200ms or less. C# can do that. Note that for most non-trivial programs you'll end up losing performance writing assembly over using a higher level language, because people have such a hard time writing effective code at that level. Most all high performance code is written in C/C++, which for today's compilers has almost 100% efficiency mapping to assembly, but is much easier to write *correct* code in. – Servy Mar 18 '13 at 21:19
  • If performance is of any concern you likely should *not be copying* objects at all. Any code that you execute just for the sake of it slows your program down... If you still want to copy - @Servy (+1) provides some good suggestions. – Alexei Levenkov Mar 18 '13 at 21:21
  • @AlexeiLevenkov If performance truly matters you do want to minimize copying as much as possible, but it's not always possible. Obviously we don't know enough about the program to know if it could be removed in the OP's case though, or what it might take to avoid the copying. I would say though that most people end up copying data way more than they need to, and performance can suffer as a result. – Servy Mar 18 '13 at 21:23

1 Answers1

10

If speed matters don't rely on an entirely general purpose solution (particularly in this case, as in this case the problem is so hard to solve in the general case that performance really suffers; easier problems can be solved efficiently in the general case), most of which generally rely on serialization, which is inherently not performant as a lot of time/effort is spent on string manipulation. Simply manually create the objects based on what you know their types are, and manually copy the relevant fields based on what you know needs to be copied.

Also be sure to only do a shallow copy of any immutable objects, rather than a deep copy, since there is no way to observe a difference between a shallow/deep copy.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • Watch out for "falsely immutable" objects which contain readonly (i.e. apparently immutable) references to objects that themselves are not immutable. If such objects were passed to the object holding them, they could be mutated outside the control of the containing object. The solution is to make a defensive copy of any such object, and store the copy in the containing object. – Matthew Watson Mar 18 '13 at 21:13
  • 1
    @MatthewWatson Very true. That's why you can gain so much out of a non-generalized solution (and why generalizing is so hard) is that knowing what information needs to be copied, and what can and can't have visible side effects of mutation is simply not something that can be determined automatically. A general solution is forced to make the defensive copy, but a non-general solution can know not to, and reap the performance benefits. – Servy Mar 18 '13 at 21:15