Fastest way to check if a file exist using standard C++
来源:互联网 发布:万网域名注册 .top 编辑:程序博客网 时间:2024/06/04 18:33
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>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.
0 0
- Fastest way to check if a file exist using standard C++
- Makefile – Check if a file exists using wildcard function
- Fastest way to estimate rows in a table
- Is there a way to check if android WindowManager already contains a view?
- Ant Task check if a file exists
- Simple way to check if an image bitmap is blur
- The standard way to recover the lost file.
- Fastest way to build an HTML string
- Fastest Way to Update Rows in a Large Table in SQL Server
- IDL find if a file or directory exist
- Ten Ways to Check if an Integer Is a Power Of Two in C
- Ten Ways to Check if an Integer Is a Power Of Two in C
- Ten Ways to Check if an Integer Is a Power Of Two in C
- check file exist bash script
- Check if a database and tables exist in sql server in a vb .net project
- Check if a File is in Internet Explorers Cache
- How to Determine if a file is a .Net assembly (in Delphi and C#)
- A simple way to read an XML file in Java
- SQL的case when then else end语句的用法
- JDK环境安装教程
- 《Apache Kylin v1.5部署时报错:Please make sure the user has the privilege to run hbase shell》
- 给定产生1-5的随机数,求1-7的随机数函数
- CF 219D Choosing Capital for Treeland (树形dp)
- Fastest way to check if a file exist using standard C++
- MD5加密
- iOS基础GIF图片的实现
- box2djs 一个bug world.Step运行数秒钟的调试记录
- udp一对一通信
- poj3617Best Cow Line(贪心)
- IO流——任意访问文件流
- HDU 5834 Magic boy Bi Luo with his excited tree(树形dp)
- java学习-反射