在C中提高文件IO的性能 [英] improve performance in file IO in C

查看:135
本文介绍了在C中提高文件IO的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在对它们执行堆操作之后逐个将大量的整数写入文件。我试图将排序的文件合并到一个文件中。截至目前,我正在每次操作后写入文件。我使用min heap来合并文件。

I need to write bulk of integers to a file after performing heap operations on them, one by one. I am trying to merge sorted files into a single file. As of now, I am writing to file after every operation. I am using min heap to merge files.

我的问题是 -


  1. 执行文件写入时,每次进行文件写入或一次写入多块内存块时是否访问磁盘?

  1. When performing file write, is disk accessed every time a file write is made or chunks of memory blocks are written at a time?

是否会改进性能如果我将把数据输出大小为1024或者更多,然后立即执行写入?

Will it improve performance if I'll take output of heap in an array of say size 1024 or may be more and then perform a write at once?

提前感谢您。

编辑 - 请使用setbuffer()帮助?我觉得它应该有一定的帮助。

EDIT- Will using setbuffer() help? I feel it should help for certain extend.

推荐答案


执行文件写入时,每次访问磁盘一个文件写入
的时间或者一次写入一块内存块?

When performing file write, is disk accessed every time a file write is made or chunks of memory blocks are written at a time?

这取决于内核。当您在文件描述符上调用 fsync()时,将刷新缓冲区。 fflush()只刷新在 FILE 结构中缓冲的数据,它不会刷新内核缓冲区。

This is up to the kernel. Buffers are flushed when you call fsync() on the file descriptor. fflush() only flushes the data buffered in the FILE structure, it doesn't flush the kernel buffers.


如果我将
数组中的堆输出称为1024或者更多然后执行,它会提高性能吗?一次写?

Will it improve performance if I'll take output of heap in an array of say size 1024 or may be more and then perform a write at once?

我前段时间做过测试来比较 write()的性能 fwrite()针对自定义实现,事实证明你可以通过调用 write()获得公平的加速直接使用大块。这实际上是 fwrite()所做的,但由于它必须维护的基础设施,它比自定义实现慢。至于缓冲区大小,1024肯定是太小了。 8K或其他什么表现会更好。

I made tests some time ago to compare the performance of write() and fwrite() against a custom implementation, and it turns out you can gain a fair speedup by calling write() directly with large chunks. This is actually what fwrite() does, but due to the infrastructure it has to maintain, it is slower than a custom implementation. As for buffer size, 1024 is certainly too small. 8K or something would perform better.

这篇关于在C中提高文件IO的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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