读取二进制数据和纯文本数据哪个更快?
我有一些数据,我知道其确切结构。它必须一秒一秒地插入到文件中。 这些结构体包含 double 字段,但它们具有不同的名称。每秒必须将相同数量的结构写入文件,
事情是.. 在读取数据时,这是一个更好的方法
1- 将结构转换为字节,然后插入它,同时索引标记第二个结束的字节
2- 写入 CSV 数据并索引标记第二个结束的
字节 数据是从文件中随机请求的。 因此,在这两种情况下,我都会将 FileStream 的位置设置为第二个字节。
在第一种情况下,我将为该秒中的每个结构使用以下内容来获取
_filestream.Read(buffer, 0, buffer.Length);
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
oReturn = (object)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), _oType);
前一个的 全部数据该方法被应用 X 次,因为每秒大约有 100 个结构
在第二种情况下,我将使用 string.Split(',') 然后我将相应地填写数据,因为我知道数据的确切顺序
file.Read(buffer, 0, buffer.Length);
string val = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
string[] row = val.Split(',');
编辑 使用探查器没有显示出差异,但我无法模拟确切的现实生活场景,因为文件大小可能会变得非常大。我现在正在寻找理论信息
I have some data that I know its exact structure. It has to be inserted in files second by second.
The structs contain fields of double, but they have different names. The same number of struct have to be written to file every second
The thing is ..
Which is a better appraoch when it comes to reading the data
1- Convert the Structs to bytes then insert it while indexing the byte that marks the end of the second
2- Writing CSV data and index the byte that marks the end of second
The data is requested at random basis from the file.
So in both cases I will set the position of the FileStream to the byte of the second.
In the first case I will use the following for each of the struct in that second to get the whole data
_filestream.Read(buffer, 0, buffer.Length);
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
oReturn = (object)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), _oType);
the previous approach is applied X number of times because there's around 100 struct every second
In the second case I will use string.Split(',') then I will fill in the data accordingly since I know the exact order of my data
file.Read(buffer, 0, buffer.Length);
string val = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
string[] row = val.Split(',');
edit
using the profiler is not showing a difference, but I cannot simulate the exact real life scenario because the file size might get really huge. I am looking for theoratical information for now
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论