性能 - NSValue 中的结构与容器对象
在我需要将所有数据成员保存在 NSDictionary
中的情况下,将结构(自定义类型,甚至标量,即 CGPoint
)放入我自己的结构中是否更有意义包装器(不是 NSValue
),这样我就可以避免每次获取或设置成员时对其进行编码/解码的开销?
对于大型结构(16 个浮点),节省的费用在 IMO 中非常显着。但即使使用 CGPoint,我也能节省 4 个字节的复制时间以及编码/解码时间。
In a situation where I need to save all data members in a NSDictionary
, does it make more sense to put structs (custom types, or even scalars i.e. CGPoint
) in my own wrapper (not NSValue
), so I can avoid the overhead of encoding/decoding it every time I get or set the member?
For large structs (16 floats) the savings is IMO significant. But even with a CGPoint
I'd be saving 4 bytes of copying plus encoding/decoding time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不针对您的具体情况进行测量呢?这是找到答案的唯一真正可靠的方法。
如果这两个选项都不够,您可以通过设置 相应的值回调 或采用 C++
std::map
/< code>std::tr1::unordered_map 进行旋转(如果您不介意混合使用 C++)。Why not just measure it for your specific case? That is the only really reliable way to find it out.
If both options are not sufficient you could look into
CFDictionary
with pointers to plain structs by setting the value callbacks accordingly or take a C++std::map
/std::tr1::unordered_map
for a spin (if you don't mind mixing in C++ that is).您实际上可能更进一步:完全放弃该结构并使其成为模型对象。然后,您可以将逻辑(例如,计算属性)集成到其中,并赋予它以任何相关格式对自身进行编码和解码的能力,以及将其放入集合中。
You might actually go a step further: Abandon the struct entirely and make it a model object. You can then integrate logic (e.g., computed properties) into it and give it the ability to encode and decode itself in any relevant formats, as well as put it into collections.
好吧,NSDictionary 类型无论如何都需要对象作为其成员,因此您不能在 NSDictionary 中存储结构。您必须将其包装在容器对象中。
Well, NSDictionary types require objects as their members anyway, so you can't store a struct in an NSDictionary. You'd have to wrap it in a container object.