久久久久久久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)嗎?)
主站蜘蛛池模板: 欧美日韩在线看 | 美女毛片 | 欧美国产日韩在线观看 | www.国产一区 | 91精品国产乱码久久久久久 | 午夜视频网站 | 日韩av手机在线观看 | 成人免费视频在线观看 | 久久国产精品精品国产色婷婷 | 91丨九色丨国产在线 | 国产精品福利在线 | 久久久www成人免费无遮挡大片 | 农村妇女毛片精品久久久 | 日本公妇乱淫xxxⅹ 国产在线不卡 | 最近中文字幕第一页 | 午夜在线免费观看 | 国产欧美精品在线观看 | 在线免费观看黄色 | 美国一级毛片a | 国产精品自拍视频 | 欧美videosex性极品hd | 国产成人精品亚洲日本在线观看 | 国产高清免费视频 | 青青草精品视频 | 日韩欧美三区 | www国产成人免费观看视频,深夜成人网 | 本道综合精品 | 羞羞视频免费观 | 99久久99| 国产欧美精品一区二区色综合朱莉 | 久久久精 | 精品视频在线播放 | 午夜小视频在线观看 | 欧美中文字幕一区 | 日韩视频一区在线观看 | 国产区精品 | 国内精品久久影院 | 久久精品一区二 | 日韩高清电影 | 精品1区2区3区4区 | 中文字幕一区二区三区精彩视频 |