{"id":278804,"date":"2018-02-23T20:05:43","date_gmt":"2018-02-24T01:05:43","guid":{"rendered":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/uncategorized\/c-deep-cloning-objects-stack-overflow-2.php"},"modified":"2018-02-23T20:05:43","modified_gmt":"2018-02-24T01:05:43","slug":"c-deep-cloning-objects-stack-overflow-2","status":"publish","type":"post","link":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/cloning\/c-deep-cloning-objects-stack-overflow-2.php","title":{"rendered":"c# &#8211; Deep cloning objects &#8211; Stack Overflow"},"content":{"rendered":"<p><p>Q. Why would I choose this answer?<\/p>\n<p>In other words, go with another answer unless you have a performance bottleneck that needs fixing, and you can prove it with a profiler.<\/p>\n<p>The following method of performing a deep clone is:<\/p>\n<p>For ultimate speed, you can use Nested MemberwiseClone to do a deep copy. Its almost the same speed as copying a value struct, and is much faster than (a) reflection or (b) serialization (as described in other answers on this page).<\/p>\n<p>Note that if you use Nested MemberwiseClone for a deep copy, you have to manually implement a ShallowCopy for each nested level in the class, and a DeepCopy which calls all said ShallowCopy methods to create a complete clone. This is simple: only a few lines in total, see the demo code below.<\/p>\n<p>Here is the output of the code showing the relative performance difference for 100,000 clones:<\/p>\n<p>Using Nested MemberwiseClone on a class almost as fast as copying a struct, and copying a struct is pretty darn close to the theoretical maximum speed .NET is capable of.<\/p>\n<p>To understand how to do a deep copy using MemberwiseCopy, here is the demo project that was used to generate the times above:<\/p>\n<p>Then, call the demo from main:<\/p>\n<p>Again, note that if you use Nested MemberwiseClone for a deep copy, you have to manually implement a ShallowCopy for each nested level in the class, and a DeepCopy which calls all said ShallowCopy methods to create a complete clone. This is simple: only a few lines in total, see the demo code above.<\/p>\n<p>Note that when it comes to cloning an object, there is is a big difference between a \"struct\" and a \"class\":<\/p>\n<p>See differences between value types and references types.<\/p>\n<p>One excellent use case for this code is feeding clones of a nested class or struct into a queue, to implement the producer \/ consumer pattern. <\/p>\n<p>This works extremely well in practice, and allows us to decouple many threads (the producers) from one or more threads (the consumers).<\/p>\n<p>And this method is blindingly fast too: if we use nested structs, it's 35x faster than serializing\/deserializing nested classes, and allows us to take advantage of all of the threads available on the machine.<\/p>\n<p>Apparently, ExpressMapper is as fast, if not faster, than hand coding such as above. I might have to see how they compare with a profiler. <\/p>\n<p><!-- Auto Generated --><\/p>\n<p>Continue reading here: <\/p>\n<p><a target=\"_blank\" rel=\"nofollow\" href=\"https:\/\/stackoverflow.com\/questions\/78536\/deep-cloning-objects\" title=\"c# - Deep cloning objects - Stack Overflow\">c# - Deep cloning objects - Stack Overflow<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p> Q.  <a href=\"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/cloning\/c-deep-cloning-objects-stack-overflow-2.php\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":57,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"limit_modified_date":"","last_modified_date":"","_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[431597],"tags":[],"class_list":["post-278804","post","type-post","status-publish","format-standard","hentry","category-cloning"],"modified_by":null,"_links":{"self":[{"href":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/wp-json\/wp\/v2\/posts\/278804"}],"collection":[{"href":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/wp-json\/wp\/v2\/users\/57"}],"replies":[{"embeddable":true,"href":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/wp-json\/wp\/v2\/comments?post=278804"}],"version-history":[{"count":0,"href":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/wp-json\/wp\/v2\/posts\/278804\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/wp-json\/wp\/v2\/media?parent=278804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/wp-json\/wp\/v2\/categories?post=278804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.euvolution.com\/futurist-transhuman-news-blog\/wp-json\/wp\/v2\/tags?post=278804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}