0

I am using below c++ code to copy a file (size is around 1.5-2 gb) from one location to another.

#include <fstream>

int main()
{
    std::ifstream  src("source_path/file.mp4", std::ios::binary);
    std::ofstream  dst("destination_path/fileCopy.mp4",   std::ios::binary);

    dst << src.rdbuf();
}

I have taken this code from here Copy a file in a sane, safe and efficient way

This code is taking around 45 seconds to copy the file from source to destination. I know here the rdbuf must have some buffer size associated with it, Actually I want to increase the buffer size for more better performance, How can I increase the buffer ?

Vinay Kumar
  • 674
  • 2
  • 9
  • 21

1 Answers1

0

Here is what I use to improve the read performance by about 1.8X:

// Open file for binary read.
ifstream in_file(file_name, ios::binary | ios::in);
// Proceed if file was successfully opened.
if (in_file.is_open()) {
    // Tried buffer size for 16 transfers. Seems a bit slower than 64.
    //const size_t bufsize = max(4096, 2 << (int32_t)log2(block_size >> 4));
    // Set buffer size for 64 transfers.
    const size_t bufsize = max(4096, 2 << (int32_t)log2(block_sizes[0] >> 6));
    auto buf = make_unique<char[]>(bufsize);
    in_file.rdbuf()->pubsetbuf(buf.get(), bufsize);
    // Create block for holding lines.
    auto lmemblock = make_unique<char[]>(block_sizes[i]);
    // Move file pointer to start of block.
    in_file.seekg(offsets[i]);
    // Read memblock of data into memory.
    in_file.read(lmemblock.get(), block_sizes[i]);

The key line is in_file.rdbuf()->pubsetbuf(buf.get(), bufsize); This upsizes the default buffer size which for my case significantly improved performance.