Boost logo

Boost :

From: Dylan Nicholson (dylan_nicholson_at_[hidden])
Date: 2002-03-19 00:37:33

If we definitely decide to put in a "copy_file" routine into boost::filesystem,
then believe it or not, the following single line of code is remarkably
efficient (at least under HP aCC 3.33, which is what I tested it with):

bool copy(const char* src, const char* dest)
        return std::ofstream(dest) << std::ifstream(src).rdbuf();

The following is almost twice as slow (!):

bool copy2(const char* src, const char* dest)
  std::ifstream ifs(src);
  ifs.seekg(0, std::ios_base::end);
  std::vector<char> buf(ifs.tellg());
  ifs.seekg(0, std::ios_base::beg);
  return[0], buf.size()) &&
    std::ofstream(dest).write(&buf[0], buf.size());

I tested this copying a local ~200Kb file 50 times, so I don't know how
representative that is. All sorts of factors might be at play, but the results
were consistent.

Dylan - Yahoo! Movies
- Vote for your nominees in our online Oscars pool.

Boost list run by bdawes at, gregod at, cpdaniel at, john at