久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用標準 C++/C++11,14,17/C 檢查文件是否存在的最快

Fastest way to check if a file exist using standard C++/C++11,14,17/C?(使用標準 C++/C++11,14,17/C 檢查文件是否存在的最快方法?)
本文介紹了使用標準 C++/C++11,14,17/C 檢查文件是否存在的最快方法?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想找到檢查標準 C++11、14、17 或 C 中是否存在文件的最快方法.我有數千個文件,在對它們執行某些操作之前,我需要檢查是否所有文件都存在存在.我可以在下面的函數中寫什么來代替 /* SOMETHING */ ?

I would like to find the fastest way to check if a file exist in standard C++11,14,17, or C. I have thousands of files and before doing something on them I need to check if all of them exist. What can I write instead of /* SOMETHING */ in the following function?

inline bool exist(const std::string& name)
{
    /* SOMETHING */
}

推薦答案

好吧,我拼湊了一個測試程序,該程序運行這些方法中的每一種 100,000 次,一半在存在的文件上,一半在不存在的文件上.

Well I threw together a test program that ran each of these methods 100,000 times, half on files that existed and half on files that didn't.

#include <sys/stat.h>
#include <unistd.h>
#include <string>
#include <fstream>

inline bool exists_test0 (const std::string& name) {
    ifstream f(name.c_str());
    return f.good();
}

inline bool exists_test1 (const std::string& name) {
    if (FILE *file = fopen(name.c_str(), "r")) {
        fclose(file);
        return true;
    } else {
        return false;
    }   
}

inline bool exists_test2 (const std::string& name) {
    return ( access( name.c_str(), F_OK ) != -1 );
}

inline bool exists_test3 (const std::string& name) {
  struct stat buffer;   
  return (stat (name.c_str(), &buffer) == 0); 
}

運行 100,000 次調用的總時間平均超過 5 次運行的結果,

Results for total time to run the 100,000 calls averaged over 5 runs,

<頭>
方法時間
exists_test0 (ifstream)0.485s
exists_test1 (FILE fopen)0.302s
exists_test2 (posix access())0.202s
exists_test3 (posix stat())0.134s
Method Time
exists_test0 (ifstream) 0.485s
exists_test1 (FILE fopen) 0.302s
exists_test2 (posix access()) 0.202s
exists_test3 (posix stat()) 0.134s

stat() 函數在我的系統(Linux,使用 g++ 編譯)上提供了最佳性能,標準的 fopen 調用是如果您出于某種原因拒絕使用 POSIX 函數,那么您最好的選擇.

The stat() function provided the best performance on my system (Linux, compiled with g++), with a standard fopen call being your best bet if you for some reason refuse to use POSIX functions.

這篇關于使用標準 C++/C++11,14,17/C 檢查文件是否存在的最快方法?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉換為 HSV 并將 HSV 轉換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類型變量轉換為字符串?)
When to use inline function and when not to use it?(什么時候使用內聯函數,什么時候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相關嗎?)
主站蜘蛛池模板: 日韩久久综合 | 亚洲国产片 | 国产精品入口夜色视频大尺度 | 成人免费毛片aaaaaa片 | 免费黄网站在线观看 | www.四虎影视 | 久久久一本 | 黄色一级大片在线免费看国产一 | 深夜福利网 | 中文在线免费看视频 | 成人在线小视频 | 日韩精品一区在线观看 | 欧美精品日韩 | 日本高清网站 | 亚洲91精品 | 97国产视频| 中文字幕超清在线观看 | 精品在线播放 | 超碰777| 二色av | 草草在线观看 | www.色综合 | 国产真实乱人偷精品 | 四虎欧美 | 久久久久久久久国产 | 五月天婷婷影院 | 久久99九九 | 日韩性视频 | 久久精彩 | 日韩成人一区 | 亚洲私人影院 | 国产一区中文字幕 | 亚洲成人a v | 91欧美激情一区二区三区成人 | 欧美成人免费在线视频 | 色香蕉网 | 久久久一区二区三区 | 黄色一级大片在线免费看产 | 婷婷激情综合网 | 免费中文字幕日韩欧美 | 国产精品伦一区二区三级视频 |