在 c++ 中创建大文件的最快方法?
使用 C++ 创建大约 50 - 100 MB 的平面文本文件 内容“添加第一行”应该被插入到文件中 400 万次
Create a flat text file in c++ around 50 - 100 MB
with the content 'Added first line' should be inserted in to the file for 4 million times
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用旧式文件io
fopen 文件进行写入。
fseek 到所需的文件大小 - 1.
fwrite 单字节
fclose 文件
using old style file io
fopen the file for write.
fseek to the desired file size - 1.
fwrite a single byte
fclose the file
创建特定大小的文件的最快方法是使用
creat()
或open()
创建一个零长度文件,然后使用更改大小>chsize()
。 这将简单地在磁盘上为文件分配块,内容将是这些块中发生的任何内容。 由于不需要进行缓冲区写入,因此速度非常快。The fastest way to create a file of a certain size is to simply create a zero-length file using
creat()
oropen()
and then change the size usingchsize()
. This will simply allocate blocks on the disk for the file, the contents will be whatever happened to be in those blocks. It's very fast since no buffer writing needs to take place.不确定我理解这个问题。 您想确保文件中的每个字符都是可打印的 ASCII 字符吗? 如果是这样,那这个呢? 用“abcdefghabc ....”填充文件
Not sure I understand the question. Do you want to ensure that every character in the file is a printable ASCII character? If so, what about this? Fills the file with "abcdefghabc...."
您没有提到操作系统,但我假设创建/打开/关闭/写入可用。
为了真正高效地写入并假设 4k 页和磁盘块大小以及重复的字符串:
这绕过了 fopen() 和朋友的缓冲,这有好有坏:它们的缓冲意味着它们又好又快,但它们仍然不会像这样高效,因为它没有使用缓冲区的开销。
这可以很容易地用 C++ 或 C 编写,但为了提高效率,假设您将使用 POSIX 调用而不是 iostream 或 stdio,因此它超出了核心库规范。
You haven't mentioned the OS but I'll assume creat/open/close/write are available.
For truly efficient writing and assuming, say, a 4k page and disk block size and a repeated string:
This bypasses the buffering of fopen() and friends, which is good and bad: their buffering means that they're nice and fast, but they are still not going to be as efficient as this, which has no overhead of working with the buffer.
This can easily be written in C++ or C, but does assume that you're going to use POSIX calls rather than iostream or stdio for efficiency's sake, so it's outside the core library specification.
我遇到了同样的问题,在 Windows 上非常快地创建了约 500MB 的文件。
传递给 fwrite() 的缓冲区越大,速度就越快。
现在至少在我的 Win7 上,MinGW 几乎可以立即创建文件。
与一次写入 1 个字节的 fwrite() 相比,这将在 10 秒内完成。
通过 4k 缓冲区将在 2 秒内完成。
I faced the same problem, creating a ~500MB file on Windows very fast.
The larger buffer you pass to fwrite() the fastest you'll be.
Now at least on my Win7, MinGW, creates file almost instantly.
Compared to fwrite() 1 byte at time, that will complete in 10 Secs.
Passing 4k buffer will complete in 2 Secs.
在 C++ 中创建大文件的最快方法?
好的。 我认为最快的方法意味着运行时间最短的方法。
用 C++ 创建一个大约 50 - 100 MB 的平面文本文件,内容“添加第一行”应插入到该文件中 400 万次。
使用旧样式文件预分配文件 io
预分配文件使用旧式文件 io
这是我最好的猜测。
我确信有很多方法可以做到这一点。
Fastest way to create large file in c++?
Ok. I assume fastest way means the one that takes the smallest run time.
Create a flat text file in c++ around 50 - 100 MB with the content 'Added first line' should be inserted in to the file for 4 million times.
preallocate the file using old style file io
preallocate the file using old style file io
That's my best guess.
I'm sure there are a lot of ways to do it.