博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Fastest way to check if a file exist using standard C++
阅读量:2442 次
发布时间:2019-05-10

本文共 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/

你可能感兴趣的文章
使用PHP从Access数据库中提取对象,第2部分
查看>>
openbiz_Openbiz Cubi:健壮PHP应用程序框架,第1部分
查看>>
使用PHP从Access数据库中提取对象,第1部分
查看>>
使用云waf的案例_9种流行的云使用案例
查看>>
类集合转换类集合_PHP中的集合类
查看>>
使用SimplePie消费Feed
查看>>
运算符二进制_基本转换和二进制运算符
查看>>
SitePoint播客#121:在线社区圆桌会议第2部分
查看>>
pchart_用pChart绘制图表
查看>>
Git简介,第1部分
查看>>
微信 获取地理位置名称_使用地理名称的目标地理位置
查看>>
graph api_与Facebook Graph API集成
查看>>
本地应用程序_本地化PHP应用程序的“正确方法”,第4部分
查看>>
mfc单文档绘制世界坐标系_文档使世界运转
查看>>
防止跨站请求伪造(CSRF)
查看>>
科创板 云计算业务公司_云计算对您的业务意味着什么
查看>>
SitePoint播客#80:与澳大利亚团队的FullCodePress
查看>>
BigCommerce和Volusion:比较设置和移动
查看>>
使用codeigniter_使用CodeIgniter探索面向方面的编程,第2部分
查看>>
用Wufoo创建简单表单:开发人员的观点
查看>>