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

使用標(biāo)準(zhǔn) C++/C++11,14,17/C 檢查文件是否存在的最快

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

問題描述

我想找到檢查標(biāo)準(zhǔn) C++11、14、17 或 C 中是否存在文件的最快方法.我有數(shù)千個(gè)文件,在對(duì)它們執(zhí)行某些操作之前,我需要檢查是否所有文件都存在存在.我可以在下面的函數(shù)中寫什么來代替 /* 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 */
}

推薦答案

好吧,我拼湊了一個(gè)測(cè)試程序,該程序運(yùn)行這些方法中的每一種 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); 
}

運(yùn)行 100,000 次調(diào)用的總時(shí)間平均超過 5 次運(yùn)行的結(jié)果,

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

<頭>
方法時(shí)間
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() 函數(shù)在我的系統(tǒng)(Linux,使用 g++ 編譯)上提供了最佳性能,標(biāo)準(zhǔn)的 fopen 調(diào)用是如果您出于某種原因拒絕使用 POSIX 函數(shù),那么您最好的選擇.

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.

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

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

相關(guān)文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉(zhuǎn)換為 HSV 并將 HSV 轉(zhuǎn)換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類型變量轉(zhuǎn)換為字符串?)
When to use inline function and when not to use it?(什么時(shí)候使用內(nèi)聯(lián)函數(shù),什么時(shí)候不使用?)
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 仍然相關(guān)嗎?)
主站蜘蛛池模板: 午夜视频网站 | 亚洲国产网址 | 免费观看黄a一级视频 | 美女在线视频一区二区三区 | com.色.www在线观看 | 欧美色a v | 午夜视频免费 | 一区二区三区不卡视频 | 好姑娘影视在线观看高清 | 亚洲成在线观看 | 成人三级视频 | 欧美国产日韩在线观看 | 国产精品视频一区二区三区四区国 | 激情综合五月 | 国产免费又黄又爽又刺激蜜月al | 精品91 | 精品国产乱码久久久久久闺蜜 | 国产在线精品一区二区三区 | 国产精品视频免费观看 | 精品久久精品 | 国产成人网 | 成人一区二区三区 | 国产99热精品 | 久草视 | 91精品国产日韩91久久久久久 | 男女免费在线观看视频 | 久精品久久 | 欧美在线亚洲 | 日韩av在线一区二区 | 激情六月天 | 国产精品99久久久久久宅男 | 久久久久久久av麻豆果冻 | av国产精品毛片一区二区小说 | 免费电影av | 99国产精品99久久久久久 | 毛片在线免费播放 | 精精国产xxxx视频在线播放 | 精品国产乱码久久久久久闺蜜 | 日韩视频一区二区 | 日本精品一区二区 | 日韩精品免费在线观看 |