大佬教程收集整理的这篇文章主要介绍了C写选项,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有一些STL方式来实现它?
我不想基于WinAPI编写代码.
我的目标是禁用OS缓存并使用不同的块大小执行写入,以获得与存储性能相关的数据.
我无法使用标准基准测试工具,因为目标是了解如何优化我必须依赖的特定存储的写层设置.@H_674_10@
@update
这是@L_534_3@mWE,我希望在更改blk_size的值时看到不同的保存时间:@H_674_10@
#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; }
先感谢您@H_674_10@
以上是大佬教程为你收集整理的C写选项全部内容,希望文章能够帮你解决C写选项所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。