日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

C++风格与C风格文件读写效率测试-vs2015,vs2017

發(fā)布時(shí)間:2025/3/18 c/c++ 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++风格与C风格文件读写效率测试-vs2015,vs2017 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

C++風(fēng)格與C風(fēng)格文件讀寫效率測(cè)試-vs2015,vs2017

1 void test_write() 2 { 3 const int TEST_SIZE = 100000000; 4 const char* c_plus_write_file = "H://c_plus_write_file.txt"; 5 const char* c_write_file = "g://c_write_file.txt"; 6 7 cout << "Test size :" << TEST_SIZE << endl; 8 //c++ style writing file 9 ofstream of(c_plus_write_file); 10 //assert(of); 11 time_t start, end; 12 start = clock(); 13 for (int i = 0; i < TEST_SIZE; ++i) 14 { 15 char tmp[1]; 16 tmp[0] = char(i); 17 of.write(tmp, 1); 18 } 19 end = clock(); 20 of.close(); 21 cout << "C++ style: " << end - start << " ms" << endl; 22 //c style writing file 23 FILE* fp; 24 fopen_s(&fp, c_write_file, "w"); 25 start = clock(); 26 for (int i = 0; i < TEST_SIZE; ++i) 27 { 28 char tmp[1]; 29 tmp[0] = char(i); 30 fwrite(tmp, 1, 1, fp); 31 } 32 end = clock(); 33 fclose(fp); 34 cout << "C style: " << end - start << " ms" << endl; 35 cin.get(); 36 } 37 38 //機(jī)器配置: 39 //vs2015,vs2017 40 //intel(R)Core(TM)i7-6700HQ CPU@2.6GHZ 2.59GHZ 41 //16.0GB內(nèi)存 64位操作系統(tǒng) 42 //測(cè)試結(jié)果:BUF_SIZE越大,C++與C風(fēng)格時(shí)間差越大,數(shù)據(jù)如下: 43 //BUF_SIZE= 1000: c++平均90ms c平均80ms 44 //BUF_SIZE= 100000000: c++平均70ms c平均30ms 45 //這是讀取測(cè)試,對(duì)于寫入操作二者相關(guān)則更顯著:BUF_SIZE= 100000000: c++平均25秒 c平均20秒 46 void test_read() 47 { 48 const char* read_file = "g://c_write_file.txt"; 49 const int BUF_SIZE = 100000000; 50 //char buf[BUF_SIZE]; 51 char* buf = new char[BUF_SIZE]; 52 time_t start, end; 53 54 //c style writing file 55 FILE* fp = fopen(read_file, "rb"); 56 assert(fp); 57 start = clock(); 58 int len = 0; 59 do 60 { 61 len = fread(buf, 1, BUF_SIZE, fp); 62 //cout<<len<<endl; 63 } while (len != 0); 64 end = clock(); 65 fclose(fp); 66 cout << "C style: " << end - start << " ms" << endl; 67 68 //c++ style writing file 69 ifstream ifs(read_file, ios::binary); 70 assert(ifs); 71 start = clock(); 72 while (!ifs.eof()) 73 { 74 ifs.read(buf, BUF_SIZE); 75 } 76 end = clock(); 77 ifs.close(); 78 cout << "C++ style: " << end - start << " ms" << endl; 79 80 delete[] buf; 81 cin.get(); 82 }

?

posted on 2018-02-17 10:59 時(shí)空觀察者9號(hào) 閱讀(...) 評(píng)論(...) 編輯 收藏

總結(jié)

以上是生活随笔為你收集整理的C++风格与C风格文件读写效率测试-vs2015,vs2017的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。