目前在弄文件缓存的时候用到了判定文件存在与否,is_file()还是file_exists()呢?is_file和file_exists两者效率比较起来,谁的运行速度更快呢?还是做个测试吧:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
$start_time = get_microtime();
for($i=0;$i<10000;$i++)//默认1万次,可手动修改
{
if(is_file('test.txt')) {
//do nothing;
}
}
echo 'is_file-->'.(get_microtime() - $start_time).'<br>';
$start_time = get_microtime();
for($i=0;$i<10000;$i++)//默认1万次,可手动修改
{
if(file_exists('test.txt')) {
 //do nothing;
}
}
echo 'file_exits-->'.(get_microtime() - $start_time).'<br>';
function get_microtime()//时间
{
list($usec$sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
?>

测试结果:

当文件存在时:
运行1万次:
is_file–>0.0067121982574463
file_exits–>0.11532402038574

运行10万次:
is_file–>0.069056034088135
file_exits–>1.1521670818329

当运行100万次:
is_file–>0.6924250125885
file_exits–>11.497637987137

当文件不存在时:

运行1万次:
is_file–>0.72184419631958
file_exits–>0.71474003791809

运行10万次:
is_file–>7.1535291671753
file_exits–>7.0911409854889

当运行100万次:
is_file–>72.042867183685
file_exits–>71.789203166962

超过1分钟了,别忘了在php第一行加句:
set_time_limit(120);//时间限制120秒

结论:

is_file()和file_exists()效率比较,结果当文件存在时,is_file函数比file_exists函数速度快14倍,当文件不存在时,两者速度相当。同理,当文件目录存在时,is_dir()比file_exists()快18倍。不存在时两者效率相当。PHP的file_exists = is_dir + is_file。
* 如果要判断目录是否存在,请优先考虑函数 is_dir(directory)
* 如果要判断文件是否存在,请优先考虑函数 is_file(filepath)

is_dir()对比file_exists()测试结果:

当目录存在时,运行1万次
is_dir–>0.0058560371398926
file_exits–>0.11063098907471
当目录不存在时,运行1万次
is_dir–>0.7159481048584
file_exits–>0.71305584907532

is_file和file_exists效率比较的更多相关文章

  1. 黄聪:is_file和file_exists效率比较

    目前在弄文件缓存的时候用到了判定文件存在与否,is_file()还是file_exists()呢?is_file和file_exists两者效率比较起来,谁的运行速度更快呢?还是做个测试吧: 1 2 ...

  2. is_file,is_dir,file_exists

    is_file()和file_exists()效率比较,结果当文件存在时,is_file函数比file_exists函数速度快14倍,当文件不存在时,两者速度相当.同理,当文件目录存在时,is_dir ...

  3. PHP中file_exists与is_file、is_dir的区别,以及执行效率的比较 转自#冰雪傲骨#

    PHP中file_exists与is_file.is_dir的区别,以及执行效率的比较   判断文件是否存在,有2个常用的PHP函数:is_file 和 file_exists, 判断文件夹是否存在, ...

  4. PHP中file_exists与is_file、is_dir的区别,以及执行效率的比较

    判断文件是否存在,有2个常用的PHP函数:is_file 和 file_exists, 判断文件夹是否存在,有2个常用PHP函数:is_dir 和 file_exists, 即 file_exists ...

  5. php判断文件存在是用file_exists 还是 is_file

    From: http://www.php100.com/html/php/hanshu/2013/0905/4672.html [导读] 在写程序时发现在判断文件是否存在时,有两种写法,有的人用了is ...

  6. php 使用 file_exists 还是 is_file

    Jesns 提出 file_exists 比较老了,建议使用 is_file 来判断文件. 经过我的测试,is_file 果然快很多,以后可以改 is_file 来判断文件. 还有相关链接: is_f ...

  7. php file_exists无效解决办法

    一:is_file 和 file_exists 的区别:当文件存在时:is_file 比 file_exists快了N倍当文件不存在时:is_file 比 file_exists慢总之一句话:file ...

  8. php 文件下载

    public function down() { header("Content-type:text/html;charset=utf-8"); $file_name = I('g ...

  9. php 判断文件/目录是否存的方法

    涉及函数 is_file(), is_dir() , file_exists() is_file() 判断文件是否存在 is_dir() 判断目录是否存在 file_exists() 既可用于判断文件 ...

随机推荐

  1. Linux 与 CONE NAT 和 Symmetric NAT

    http://alexanderlaw.blog.hexun.com/31883661_d.html 1. NAT 的划分 RFC3489 中将 NAT 的实现分为四大类: 1. Full Cone ...

  2. ArcGIS Engine -- 常用方法

    空间关系 计算两点间距离 feature平移 计算范围 得到点集合的n倍Envelope范围 查询 查询要素,返回多个要素union后的Geometry 查找图层 得到地图上图层列表 根据名称在地图上 ...

  3. iOS & Mac 调试命令(VMMap&Top)

    vmmap & top命令解析内存 Is there any way to query the address space of a process on Mac? On Windows, w ...

  4. MyBatis学习总结_15_定制Mybatis自动代码生成的maven插件

    ==================================================================================================== ...

  5. MyBatis学习总结_07_Mybatis缓存

    一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Se ...

  6. Model的验证

    ModelValidator与ModelValidatorProvider ModelValidator public abstract class ModelValidator { public v ...

  7. Tomcat运行DOM4J的时候报ClassNotFoundException

    WEB应用中一个模块用到了DOM4J,加载到TOMCAT中运行,报错如下(给出部分StackTrace): java.lang.ClassNotFoundException: org.dom4j.Do ...

  8. 运行java -version报cannot restore segment prot after reloc: Permission denied

    linux 安装jdk1.6后,运行java -version,没有出现相关的版本信息,而是出现了以下错误: dl failure on line 685Error: failed /usr/loca ...

  9. A - 畅通工程

    A - 畅通工程 Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  10. MongoDB操作(.net)

    一.查询 var doc = new Document(); 1.模糊查询var reg = new MongoRegex(".*" + qry.名称 + ".*&quo ...