我正在尝试使用std :: ofstream打开文件进行写入,我想将其设置为直写模式(即使用CreateFile Win API提供的“FILE_FLAG_WRITE_THROUGH”)。
是否有一些STL方法可以实现它? 我不想基于WinAPI编写代码。 我的目标是禁用OS缓存并使用不同的块大小执行写入,以获得与存储性能相关的数据。 我无法使用标准基准测试工具,因为目标是了解如何优化我必须依赖的特定存储的写层设置。
@Update 这是一个MWE,我希望在更改blk_size的值时看到不同的保存时间:
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <vector>
#include <ctime>
std::vector<unsigned char>
GenerateRandomData(long numBytes)
{
std::vector<unsigned char> res(numBytes);
std::srand(std::time(0));
for (int i = 0; i < numBytes; ++i)
res[i] = static_cast<unsigned char>(std::rand() % 256);
return res;
}
int main(int, char)
{
// generate random data
const long dataLength = 1 * 1024 * 1024 * 1024; // 3 GB
std::vector<unsigned char> outbuf = GenerateRandomData(dataLength);
// define I/O block size (
const auto blk_size = 128 * 1024; // 128K
char blk[blk_size];
// configure output stream
std::ofstream ofs;
ofs.rdbuf()->pubsetbuf(blk, blk_size);
ofs.setf(std::ios_base::unitbuf);
// open file to write
ofs.open("output.dat", std::ofstream::binary | std::ofstream::trunc);
// write all data performing 512K I/O Operations
auto ptr_idx = 0;
auto ptr = reinterpret_cast<char*>(outbuf.data());
const auto outbuf_size = outbuf.size();
std::clock_t sw = clock();
ofs.write((const char *)&ptr[ptr_idx], outbuf_size);
ofs.flush();
ofs.close();
sw = ( clock() - sw );
double writtenBytes = static_cast<double>(outbuf.size());
double writtenMBytes = writtenBytes / (1024 * 1024);
double testSeconds = static_cast<double>(sw) / static_cast<double>(CLOCKS_PER_SEC);
double avgSpeed = writtenMBytes / testSeconds;
std::cout << "Benchmark: written " << writtenMBytes << " MB in " << testSeconds << " sec. => " << avgSpeed << "MB/s" << std::endl;
std::getchar();
return 0;
}
提前谢谢
答案 0 :(得分:1)
使用:std::unitbuf
std::ofstream outfile ("file.txt");
outfile << std::unitbuf << "Hello\n"; // immediately flushed