ofstream :: operator<<(streambuf)是一种缓慢的方式来复制文件 [英] ofstream::operator<<(streambuf) is a slow way to copy a file

查看:474
本文介绍了ofstream :: operator<<(streambuf)是一种缓慢的方式来复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个跨平台,没有外部库,复制一个文件的方式。在我的第一次通过,我想出了错误处理省略):

I need a cross-platform, no external library, way of copying a file. In my first pass I came up with (error handling omitted):

char buffer[LEN];
ifstream src(srcFile, ios::in | ios::binary);
ofstream dest(destFile, ios::out | ios::binary);

while (!src.eof()) {
  src.read(buffer, LEN);
  dest.write(buffer, src.gcount());
}

这个工作非常好,我知道它在做什么。 / strike>

This worked nicely and I knew exactly what it was doing.

然后我在stackoverflow上发现了一个帖子(对不起,现在找不到链接),我可以替换所有上面的代码: / p>

Then I found a post on stackoverflow (sorry, can't find a link right now) that says I can replace all of the above code with:

dest << src.rdbuf();

这是好的和紧凑的,但隐藏了很多关于它在做什么。它还证明是非常慢的,因为执行ofstream :: operator< <(streambuf)每次移动事物1个字符(使用snetxc()/ sputc())。

Which is nice and compact, but hides a lot about what it's doing. It also turns out to be really slow because the implementation of ofstream::operator<<(streambuf) moves things 1 character at a time (using snetxc()/sputc()).

有没有办法让我更快地做这个方法?

Is there a way for me to make this method faster? Is there a drawback to my original method?

更新:在窗口上使用operator<<(streambuf)有一些效率不高的问题。 .read()/。write()循环看起来总是比operator

Update: There's something inefficient about using operator<<(streambuf) on windows. The .read()/.write() loop looks to always perform better than operator<<.

另外,改变上面代码中缓冲区的大小不影响读写硬盘的大小。要做到这一点,你需要使用stream.rdbuf() - > pubsetbuf()设置缓冲区。

Also, changing the size of the buffer in the code above does not affect the size of the reads and writes to the hard drive. To do that you need to set the buffers using stream.rdbuf()->pubsetbuf().

推荐答案

fstream默认不缓冲。 GCC 4.5.2默认使用内部缓冲区,但我不认为这是标准要求。您已尝试使用pubsetbuf(见下文)为您的输入/输出流设置缓冲区。

I wonder if your fstream is unbuffered by default. GCC 4.5.2 by default uses an internal buffer, but I don't think that's required by the standard. Have you tried using pubsetbuf (see below) to set a buffer for your in/out streams.

如果我将LEN设置为0因此没有缓冲),花了10秒来复制1 MB文件。使用4k缓冲区,它在不到一秒钟内完成。

A quick test on my system, if I set LEN to 0 (and therefore unbuffered), it took 10 seconds to copy a 1 MB file. With a 4k buffer, it completed in less than a second.

#include <iostream>
#include <fstream>

int main() {
  using namespace std;
  const char* srcFile = "test.in";
  const char* destFile = "test.out";

  ifstream src;
  ofstream dest;

  const int LEN=8192;
  char buffer_out[LEN];
  char buffer_in[LEN];
  if (LEN) {
    src.rdbuf()->pubsetbuf(buffer_in, LEN );
    dest.rdbuf()->pubsetbuf(buffer_out, LEN);
  } else {
    src.rdbuf()->pubsetbuf(NULL, 0 );
    dest.rdbuf()->pubsetbuf(NULL, 0);
  }
  src.open(srcFile, ios::in | ios::binary);
  dest.open(destFile, ios::out | ios::binary);
  dest << src.rdbuf();

}

这篇关于ofstream :: operator&lt;&lt;(streambuf)是一种缓慢的方式来复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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