二进制文件操作VS文本模式 - 性能问题 [英] file operation in binary vs text mode -- performance concern

查看:119
本文介绍了二进制文件操作VS文本模式 - 性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在许多项目中,我看到的数据对象/结构写入到二进制模式文件,然后检索这些在二进制模式的文件再次回来了。

In many projects, I saw that data object/structure are written into file in binary mode, and then retrieve them back from the file in binary mode again.

我不知道为什么他们这样做二进制模式?文本和二进制模式之间的性能差异?如果不是,那么当使用二进制模式或文本模式

I wonder why they do it in binary mode? Any performance difference between text and binary mode? If not, then when to use binary mode or text mode?

推荐答案

二进制比较快。考虑存储在32位(4字节),如123456的整数。如果你写了这一点,为二进制(这是它是如何重新在电脑psented $ P $),将采取4个字节(忽略项目之间的填充在结构调整)。

Binary is faster. Consider an integer stored in 32 bits (4 bytes), such as 123456. If you were to write this out as binary (which is how it is represented in the computer) it would take 4 bytes (ignoring padding between items for alignment in structures).

要写为文本数,它必须被转换为字符的字符串(一些开销转换并存储到存储),然后写出来,因为有6个字符水库这将需要至少6个字节present数。这不包括任何额外的填充,例如用于对准的空间或分隔符读/单独的数据。

To write the number as text, it has to be converted to a string of characters (some overhead to convert and memory to store) and then written it out, it will take at least 6 bytes as there are 6 characters to respresent the number. This is not including any additional padding such as spaces for alignment or delimiters to read/seperate the data.

现在,如果你认为你有几千项,额外的时间就会增加,并且需要更多的空间,这将需要更长的时间在阅读,再有就是产生额外的时间,你必须经过转换回二进制存储读值到内存中。

Now if you consider it you had several thousands of items, the additional time can add up and require more space, which would take longer to read in and then there is the additonal time to convert back to binary for storage after you have read the value into memory.

的优点为文本,在于它是很容易阅读的人,而不是试图读取二进制数据或数据的六角转储。

The advantage to text, is that it is much easier to read for persons, rather then trying to read binary data or hex dumps of the data.

这篇关于二进制文件操作VS文本模式 - 性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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