日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

利用fstream进行文件拷贝测试

發布時間:2025/4/16 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用fstream进行文件拷贝测试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天翻到一個早期寫測試代碼的目錄,找到幾個以前的測試代碼,于是拿出來貼到博客中。(只是簡單的測試,并不嚴謹。注意這里windows和linux的硬件環境不一樣)

這一個是使用fstream進行文件拷貝的代碼,測試機器的環境大概如下(時間久了,機器有更新)

CPU: i7 低壓版 硬盤:兩個60G的SSD,好像是建興的 內存:8G DDR3

當時僅在Arch Linux上做了測試,今天順便在windows下做一個測試。
CentOS7_gcc4.9.4.ova其實是虛擬機的鏡像文件,1.8G大小。

代碼如下:

#include <fstream> #include <iostream> #include <iterator> #include <algorithm>bool copy_file(const char* src_file_path,const char* dst_file_path) {// 檢測目標文件是否存在{std::ifstream exsit(dst_file_path);if(exsit){std::cout<<"目標文件 "<< dst_file_path<< " 已經存在"<<std::endl;return false;}}std::ifstream fin(src_file_path,std::ios::binary);std::ofstream fout(dst_file_path,std::ios::binary);if(!fin || !fout){std::cout<<"打開源文件或目標文件失敗"<<std::endl;return false;}// rdbuf返回streambuf*// 速度比迭代器拷貝快太多// Linux下測試結果// time ./fstream_copy_file.exe CentOS7_gcc4.9.4.ova /mnt/data/CentOS7_gcc4.9.4.ova// 0.23s user 8.15s system 10% cpu 1:16.98 totalfout << fin.rdbuf();return true;/*使用迭代器進行拷貝沒有大問題,但是效率不高// time ./fstream_copy_file.exe CentOS7_gcc4.9.4.ova /mnt/data/CentOS7_gcc4.9.4.ova407.45s user 4.37s system 97% cpu 7:00.73 totalfin.unsetf(std::ios::skipws); //輸入流默認跳過空白字符,取消此設置// 使用迭代器進行拷貝std::copy(std::istream_iterator<char>(fin),std::istream_iterator<char>(),std::ostream_iterator<char>(fout,""));return true;*/ }int main(int c,char** v) {if(c != 3){printf("Usage:%s srcfile dstfile\n",v[0]);return 0;}copy_file(v[1],v[2]);return 0; }

測試結果如下:
Windows 10
Windows下使用VS2015編譯,64位版本(time命令來自git-bash)
Windows下硬盤是兩個機械硬盤之間拷貝(1T 7200轉希捷)

# 使用rdbuf拷貝,速度大概50M每秒 $ time ./fstrean_copy_file.exe /g/CentOS7_gcc4.9.4.ova /d/CentOS7_gcc4.9.4.ovareal 0m36.357s user 0m0.000s sys 0m0.015s# 使用std::copy拷貝,速度大概6M每秒 $ time ./fstrean_copy_file.exe /g/CentOS7_gcc4.9.4.ova /d/CentOS7_gcc4.9.4.ovareal 5m3.663s user 0m0.000s sys 0m0.015s

轉載于:https://www.cnblogs.com/oloroso/p/8979507.html

總結

以上是生活随笔為你收集整理的利用fstream进行文件拷贝测试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。