来来来,大神们讨论下这个问题

登疯作极 2015-11-18 09:26:14
源由:偶然看到这个帖子http://blog.csdn.net/kevin_ut/article/details/6088489#reply,里面讨论的是C++与c的IO流到底哪个更快。代码很简单,如下:
void test_write()
{
const int TEST_SIZE = 10000;
const char* c_plus_write_file = "c_plus_write_file.txt";
const char* c_write_file = "c_write_file.txt";

cout << "Test size :" << TEST_SIZE << endl;
//c++ style writing file
ofstream of(c_plus_write_file);
assert(of);
time_t start, end;
start = clock();
for (int i = 0; i < TEST_SIZE; ++i)
{
char tmp[10000];
tmp[0] = char(i);
of.write(tmp, 10000);
}
end = clock();
of.close();
cout << "C++ style: " << end - start << " ms" << endl;
//c style writing file
FILE* fp = fopen(c_write_file, "w");
start = clock();
for (int i = 0; i < TEST_SIZE; ++i)
{
char tmp[10000];
tmp[0] = char(i);
fwrite(tmp, 1, 10000, fp);
}
end = clock();
fclose(fp);
cout << "C style: " << end - start << " ms" << endl;
cin.get();
}

int main(){

test_write();

return 0;

}

结果如下:
左为g++,右为vs2013

...全文
135 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-11-20
  • 打赏
  • 举报
回复
无profiler不要谈效率!!尤其在这个云计算、虚拟机、模拟器、CUDA、多核 、多级cache、指令流水线、多种存储介质、……满天飞的时代!
ipqtjmqj 2015-11-20
  • 打赏
  • 举报
回复
参考https://www.byvoid.com/blog/fast-readfile

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧