本文共 1185 字,大约阅读时间需要 3 分钟。
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#include #include 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); }
Results for total time to run the 100,000 calls averaged over 5 runs,
Method exists_test0 (ifstream): **0.485s**Method exists_test1 (FILE fopen): **0.302s**Method exists_test2 (posix access()): **0.202s**Method exists_test3 (posix stat()): **0.134s**
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.
转载地址:http://rtiqb.baihongyu.com/