结构与类 [英] Structs versus classes

查看:131
本文介绍了结构与类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建在code 100,000个物体。它们是小的,只用2个或3个属性。我将把它们放在一个通用的列表,当他们,我会循环和检查值 A ,也许更新值 B

时更快/更好的创建这些对象的类或结构?

修改

一个。属性是值类型(除了字符串怎么想吗?)

乙。他们可能(我们还不确定)有一个validate方法

编辑2

我想知道:是堆和垃圾收集处理同样栈对象,或者做的工作不同。


解决方案

  

是不是更快以创建这些对象的类或结构?


您是谁可以决定回答这个问题的唯一的人。尝试左右逢源,措施有意义的,以用户为中心,相关的性能指标,然后你就会知道变更是否在相关场景的真实用户有意义的作用。

结构体消耗更少的堆内存(因为他们的的,更容易板结,不是因为他们是在栈)。但是,他们需要更长的时间比基准抄来抄去。我不知道你的性能指标是什么内存使用或速度;这里有一个权衡,你是谁知道它是什么的人。


  

是不是更好以创建这些对象的类或结构?


也许类,也许结构。根据经验:
如果对象是:搜索
1.小结果
2.逻辑上的不可变的值结果
3.有他们中的很多结果
然后我会考虑把它作为一个结构。否则,我会坚持用引用类型。

如果你需要一个变异结构的一些领域,通常是更好地构建返回正确设置领域一个全新的结构构造。这也许稍慢(衡量它!),但在逻辑上更容易推理。


  

在堆中,并通过垃圾收集处理同样栈对象?


后,他们是不一样的,因为的在堆栈上的对象是集合的根的。垃圾收集器并不需要问过是堆栈活在这件事情?因为这个问题的答案总是是的,这是在栈上。 (现在,你不能靠,要的保留的活着,因为堆栈实现细节的对象。抖动允许引进的优化,比如说,enregister什么通常会是一个堆栈值,然后它永远不会在堆栈上使GC不知道它仍然活着。一个enregistered对象可以收集及其后代积极,只要注册持有到它不会被再次读取。)

但垃圾回收的确实的具有治疗栈上的对象作为活的,同样的方式,它把公知的是活的活的任何对象。在堆栈上的对象可以是指需要被更让堆​​中分配的对象,因此,GC具有治疗栈对象就像生活堆中分配的对象确定现场设置的目的。但显然他们的的视为活物压实堆的目的,因为他们不是摆在首位堆。

清楚了吗?

I'm about to create 100,000 objects in code. They are small ones, only with 2 or 3 properties. I'll put them in a generic list and when they are, I'll loop them and check value a and maybe update value b.

Is it faster/better to create these objects as class or as struct?

EDIT

a. The properties are value types (except the string i think?)

b. They might (we're not sure yet) have a validate method

EDIT 2

I was wondering: are objects on the heap and the stack processed equally by the garbage collector, or does that work different?

解决方案

Is it faster to create these objects as class or as struct?

You are the only person who can determine the answer to that question. Try it both ways, measure a meaningful, user-focused, relevant performance metric, and then you'll know whether the change has a meaningful effect on real users in relevant scenarios.

Structs consume less heap memory (because they are smaller and more easily compacted, not because they are "on the stack"). But they take longer to copy than a reference copy. I don't know what your performance metrics are for memory usage or speed; there's a tradeoff here and you're the person who knows what it is.

Is it better to create these objects as class or as struct?

Maybe class, maybe struct. As a rule of thumb: If the object is :
1. Small
2. Logically an immutable value
3. There's a lot of them
Then I'd consider making it a struct. Otherwise I'd stick with a reference type.

If you need to mutate some field of a struct it is usually better to build a constructor that returns an entire new struct with the field set correctly. That's perhaps slightly slower (measure it!) but logically much easier to reason about.

Are objects on the heap and the stack processed equally by the garbage collector?

No, they are not the same because objects on the stack are the roots of the collection. The garbage collector does not need to ever ask "is this thing on the stack alive?" because the answer to that question is always "Yes, it's on the stack". (Now, you can't rely on that to keep an object alive because the stack is an implementation detail. The jitter is allowed to introduce optimizations that, say, enregister what would normally be a stack value, and then it's never on the stack so the GC doesn't know that it is still alive. An enregistered object can have its descendents collected aggressively, as soon as the register holding onto it is not going to be read again.)

But the garbage collector does have to treat objects on the stack as alive, the same way that it treats any object known to be alive as alive. The object on the stack can refer to heap-allocated objects that need to be kept alive, so the GC has to treat stack objects like living heap-allocated objects for the purposes of determining the live set. But obviously they are not treated as "live objects" for the purposes of compacting the heap, because they're not on the heap in the first place.

Is that clear?

这篇关于结构与类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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