C ++的流文件写入使用缓冲区吗? [英] Does C++ ofstream file writing use a buffer?

查看:182
本文介绍了C ++的流文件写入使用缓冲区吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是两个程序写入50,000,000字节的文件。第一个用C编写的程序利用一个缓冲区,一旦被填充到任意值,写入磁盘,然后重复这个过程,直到所有的50,000,000字节被写入。我注意到,当我增加缓冲区的大小时,程序花费的时间更少。例如,在BUFFER_SIZE = 1时,程序花了约88.0463秒,而在BUFFER_SIZE = 1024时,程序花费了约1.7773秒。我记录的最好时间是BUFFER_SIZE = 131072.随着BUFFER_SIZE的增加,我注意到它开始实际上花了一点时间。

第二个程序,用C ++编写,利用ofstream一次写一个字节。令我惊讶的是,这个程序只花费了1.87秒的时间。我预计它需要一分钟左右,就像BUFFER_SIZE = 1的C程序。显然,C ++ ofstream处理文件写入的方式与我想象的不同。根据我的数据,它的表现与BUFFER_SIZE = 512的C文件非常类似。是否使用某种幕后缓冲区?



这是C程序:

$ p $ const int NVALUES = 50000000 ; //写入文件的值
const char FILENAME [] =/ tmp / myfile;
const int BUFFER_SIZE = 8192; //写入

main()
{
int fd; //与输出文件关联的文件描述符
int i;
char writeval ='\0';
char buffer [BUFFER_SIZE];

//打开文件进行写入,并将其与文件描述符
关联//如果文件不存在则创建文件;如果存在,则将它的大小截断为0
fd = open(FILENAME,O_WRONLY | O_CREAT | O_TRUNC,S_IRUSR | S_IWUSR);

(i = 0; i< NVALUES; i ++)
{
//将字节打包成BUFFER_SIZE块
//写入一个块
buffer [i%BUFFER_SIZE] = writeval;
if((i%BUFFER_SIZE == BUFFER_SIZE-1 || i == NVALUES-1))
write(fd,buffer,i%BUFFER_SIZE + 1);

}

fsync(fd);

close(fd);



$ b

这里是C ++程序:

  int main()
{
ofstream(/ tmp / iofile2);
int i; (i = 0; i <50000000; i ++)
$ s

\0’ ;

ofs.flush();
ofs.close();

返回0;

$ / code>

谢谢您的宝贵时间。



然而,这是一个实现质量的问题。你可以在ISO标准的第27章中阅读所有关于它的内容,虽然也许更可读的来源是 C ++标准库:教程和参考 google搜索)。


Below are two programs that write 50,000,000 bytes to a file.

The first program, written in C, utilizes a buffer, that once filled to a arbitrary value, writes to disk, and then repeats that process until all 50,000,000 bytes are written. I noticed that as I increased the size of the buffer, the program took less time to run. For instance, at BUFFER_SIZE = 1, the program took ~88.0463 seconds, whereas at BUFFER_SIZE = 1024, the program only took ~1.7773 seconds. The best time I recorded was when BUFFER_SIZE = 131072. As the BUFFER_SIZE increased higher than that, I noticed that it began to actually take a little longer.

The second program, written in C++, utilizes ofstream to write one byte at a time. To my surprise, the program only took ~1.87 seconds to run. I expected it to take a minute or so, like the C program with BUFFER_SIZE = 1. Obviously, the C++ ofstream handles file writing differently than I thought. According to my data, it is performing pretty similarly to the C file with BUFFER_SIZE = 512. Does it use some sort of behind-the-scenes buffer?

Here is the C program:

const int NVALUES = 50000000; //#values written to the file
const char FILENAME[] = "/tmp/myfile";
const int BUFFER_SIZE = 8192; //# bytes to fill in buffer before writing

main()
{
    int fd;  //File descriptor associated with output file
    int i;
    char writeval = '\0';
    char buffer[BUFFER_SIZE];

    //Open file for writing and associate it with the file descriptor
    //Create file if it does not exist; if it does exist truncate its size to 0
    fd = open(FILENAME, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);

    for(i=0;i<NVALUES;i++)
    {
        //Package bytes into BUFFER_SIZE chunks 
                //and write a chunk once it is filled
        buffer[i%BUFFER_SIZE] = writeval;
        if((i%BUFFER_SIZE == BUFFER_SIZE-1 || i == NVALUES-1))
            write(fd, buffer, i%BUFFER_SIZE+1);

    }

    fsync(fd);

    close(fd);
}

Here is the C++ program:

int main()
{
    ofstream ofs("/tmp/iofile2");
    int i;

    for(i=0; i<50000000; i++)
        ofs << '\0';

    ofs.flush();
    ofs.close();

    return 0;
}

Thank you for your time.

解决方案

Yes, ostreams use a stream buffer, some subclass of an instantiation of the template basic_streambuf. The interface of basic_streambuf is designed so that an implementation can do buffering if there's an advantage in that.

However this is a quality of implementation issue. Implementations are not required to do this but any competent implementation will.

You can read all about it in chapter 27 of the ISO standard, though maybe a more readable source is The C++ Standard Library: A Tutorial and Reference (google search).

这篇关于C ++的流文件写入使用缓冲区吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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