将此堆与结构或类堆叠在一起? [英] Stack this heap with a struct or a class?

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

问题描述

我必须从文件中解析固定长度的数据.每行大约80个字段,主要是整数,双打和短字符串.因此它是从文件读取行,按长度分析行,并加载到Db.

在我看来,实用的方法是构造一个具有我需要从数据行中解析的所有字段的结构.我会使用结构而不是类来减少装箱和拆箱.有成千上万的记录.事实是,我读到的是不要使用大于几位的结构(我们说的是24个字节).这对我来说没有多大意义.堆栈足够大.是否因为是FIFO?关于此的任何想法都会很有趣.使它成为类而不是结构不是问题.
哎呀,虽然我在做,但我已经完成了现场解析,但这是一种蛮力.关于更优雅的方法有什么想法吗?
非常感谢Mike

I have to parse fixed length data from a file. It''s about 80 fields per line, mostly ints, doubles and short strings. So it is read line from file, parse line by lengths, load to Db.

What seems practical to me is to make a struct that has all the fields I need to parse from the line of data. I would use a struct rather than a class to cut down on boxing and unboxing. There are thousands of records. The thing is that what I have read is don''t use structs larger than very few bites (we''re talking 24 bytes). That doesn''t make much sense to me. The stack is big enough. Is it because it is FIFO? Any ideas about this would be interesting. It is not a problem to make it a class rather than a structure.
Heck, while I''m at it, I have the field parse done, but it''s sort of brute force. Any ideas on a more elegant method?
Thanks much, Mike

推荐答案

我认为您应该根据所需的值语义而不是数据结构可能分配的位置来决定它,因为那确实classstruct之间有什么区别.

是的,通常在堆栈上分配struct而不是总是分配struct,这不应该决定您的设计决定.

如果您可以通过相关的分析证明一种数据结构可以提高性能(例如),那么这可能会帮助您做出决定,但是只有在性能已被证明是问题的情况下才应考虑在内.

我的观点是,堆栈或堆不是class es和struct s的重要方面,值语义是.

这个人比我解释的要好一百万倍 http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx [
I think you should decide this based on the value semantics you want rather than where you think the data structure might be allocated, because that is really what is different between a class and a struct.

Yes, often it is the case that a struct is allocated on the stack but not always and it shouldn''t be what dictates your design decision.

If you can show through relevant profiling that a one type of data structure gives you a performance increase (for example) then that might help you decide, but that should only be a consideration if performance is a proven problem.

My point is, stack or heap are not the important aspects of classes and structs, the value semantics are.

This guy explains it a million times better than I can http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx[^].

Hope this helps,
Fredrik


谢谢.那里的解释很好,很有意义.
我认为它也清楚地表明我的数据将以任何一种方式在堆上,因此我应该利用该类
Thanks. Good explanation there and it makes sense.
I think it also clearly says that my data would be on the heap either way so I should take advantage of the class


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

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