MemberwiseClone是否等效于现有对象? [英] MemberwiseClone equivalent to an existing object?

查看:105
本文介绍了MemberwiseClone是否等效于现有对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于MemberwiseClone,这里有很多问题,但是我找不到确切的内容.

There are quite a few questions on here about MemberwiseClone, but I can't find anything covering this exactly.

据我了解,MemberwiseClone基本上只是复制一个对象的内存区域,将其转储到其他位置,然后将其称为该对象的新实例.显然非常快,对于大型对象,这是制作副本的最快方法.对于具有简单构造函数的小型对象,创建实例并复制各个属性的速度更快.

As I understand it, MemberwiseClone basically just copies an area of memory for an object, dumps it somewhere else and calls it a new instance of that object. Obviously very quick, and for large objects it is the quickest way to make a copy. For small objects with simple constructors, it's quicker to create an instance and copy the individual properties.

现在,我有一个紧密的循环,正在使用MemberwiseClone.由于MemberwiseClone总是创建一个新实例,这导致大量内存分配和重新分配,这对性能不利.我已经为一个非常大的对象编写了一个自定义副本,该副本将所有属性分别复制到一个现有对象上,总体上比使用MemberwiseClone快一点,

Now, I have a tight loop in which I'm using MemberwiseClone. As MemberwiseClone always creates a new instance this leads to a lot of memory allocation, and reallocation which isn't great for performance. I've written a custom copy for a very large object that copies all the properties individually onto an existing object, which overall is marginally quicker than using MemberwiseClone,

我认为,如果我可以抓住全部内存并将其转储到现有对象上,那么我将获得可观的性能提升,因为GC无需担心任何事情.希望我也能摆脱此消息:

I feel that if I could grab the whole chunk of memory and dump it onto the existing object, I'd achieve a decent performance boost as the GC wouldn't have to worry about anything. Hopefully I'll also get rid of this message:

请澄清一下,我想将属性从一个现有对象复制到另一个对象.我只需要一份浅表副本即可.

Just to clarify, I want to copy the properties from one existing object to another as quickly as possible. A shallow copy is all I need.

消息2 DA0023:GC中的(平均)%时间= 19.30; GC中的时间百分比相对较高.这表明过多的垃圾收集开销可能会影响 您的应用程序的响应能力.您可以收集.NET内存 分配数据和对象生存期信息以了解 您的应用程序使用更好的内存分配模式.

Message 2 DA0023: (Average)% Time in GC = 19.30; % Time in GC is relatively high. This indication of excessive amount of garbage collection overhead could be impacting the responsiveness of your application. You can gather .NET memory allocation data and object lifetime information to understand the pattern of memory allocation your application uses better.

我对代码安全性没有任何限制,速度是这里游戏的目标.

I have no restrictions on code safety, speed is the aim of the game here.

没有评论询问这是否真的必要,或者请谈论过早的优化.

No comments asking if this is really necessary or talking about premature optimisation please.

感谢您的帮助.

采取以下建议,将一个结构嵌入对象中并在其中存储所有属性.然后,我可以复制该结构,以一次分配复制所有属性.与逐字段复制相比,速度提高了50%以上,与每次使用MemberwiseClone创建新对象相比,速度提高了60%.

Taking the advise below, of embedding a struct within the object and storing all properties in that. I could then take a copy of that struct to copy all the properties in a single assignment. This yielded more than a 50% speed increase compared to copying field by field, and about a 60% speed increase over creating a new object every time with MemberwiseClone.

推荐答案

如果您的类不可继承,则可以将类的整个状态存储在公开字段结构中(公开字段,而不是属性!).这将要求您为所有字段名称加上结构名称的前缀,这在源代码中会很丑陋,但是访问该结构中的字段的速度将与访问直接放置在封闭类中的字段的速度一样快.但是,这样做将使您能够使用简单的赋值语句将结构从一个对象实例复制到另一个对象实例.

If your class is not inheritable, you could store the entire state of your class in an exposed-field structure (expose fields, not properties!). This would require that you prefix all field names with the name of the structure, which would be ugly in the source code, but accessing fields in that structure will be just as fast as would be accessing fields that were directly placed in the enclosing class. Doing this, however, will gain you the ability to copy the structure from one object instance into another using a simple assignment statement.

这篇关于MemberwiseClone是否等效于现有对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆