加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C写选项

发布时间:2020-12-16 07:16:08 所属栏目:百科 来源:网络整理
导读:我正在尝试使用std :: ofstream打开文件进行写入,我想将其设置为直写模式(即使用CreateFile Win API提供的“FILE_FLAG_WRITE_THROUGH”). 有没有一些STL方式来实现它? 我不想基于WinAPI编写代码. 我的目标是禁用OS缓存并使用不同的块大小执行写入,以获得与
我正在尝试使用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;
}

先感谢您

解决方法

使用:std :: unitbuf

std::ofstream outfile ("file.txt");
outfile << std::unitbuf <<  "Hellon";  // immediately flushed

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读