久久久久久久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 仍然相關嗎?)
主站蜘蛛池模板: 欧美激情第二页 | 久久a级片| 伊人国产女 | 老司机免费福利视频 | 少妇视频网站 | 香蕉视频在线观看网站 | 欧美视频免费在线观看 | 国内精品久久久久久久久 | 亚洲欧美在线观看视频 | 欧美一区二区精品 | 国产精品手机在线观看 | 午夜精品久久久久久久久久蜜桃 | 99九九久久 | 在线亚洲天堂 | 精品欧美黑人一区二区三区 | av一二三区| 蜜臀av性久久久久av蜜臀妖精 | 国产又黄又猛 | 一本一道久久a久久精品蜜桃 | 国产高清一区二区 | 青青草免费在线视频 | 日本久久久久久久久 | 玖玖视频 | 欧美 日韩 国产 成人 在线 | 久久精品三级 | 高清无码| 91精品国产乱码久久久 | 日韩黄色一级 | 手机看片国产 | 亚洲精品午夜精品 | 国产丝袜一区 | 国产在线观看网站 | 国产精品99久久久久久久久久久久 | 日本丰满少妇做爰爽爽 | 性色av一区| 欧美专区在线观看 | 国产日本在线 | 免费久久 | 亚洲国产成人在线 | www.wuye | 欧美一级网站 |