目前在弄文件缓存的时候用到了判定文件存在与否,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. spring_150803_service

    实体类: package com.spring.model; public class DogPet { private int id; private String name; private in ...

  2. lintcode:验证二叉查找树

    题目 给定一个二叉树,判断它是否是合法的二叉查找树(BST) 一棵BST定义为: 节点的左子树中的值要严格小于该节点的值. 节点的右子树中的值要严格大于该节点的值. 左右子树也必须是二叉查找树. 一个 ...

  3. Linux问题定位工具大放送

    我们在程序定位问题时,经常不知所错,但是在linux有很多强大的工具,只要我们合理利用,一定见奇效. 主要会遇到以下问题: 1 mem高 2 cpu高 3 io高 4 网络延迟高 vargrind:h ...

  4. java实现音频转换

    这里需要用到第三方 ffmpeg.exe package com.convertaudio; import java.io.File;import java.util.ArrayList;import ...

  5. Win8.1安装VirtualSVN Server发生service visualSVN Server failed to start解决办法

    Service 'VisualSVN Server' failed to start. Please check VisualSVN Server log in Event Viewer for mo ...

  6. PHP如何提取img标签属性

    extract_attrib是一个提取的图像标签属性的PHP脚本函数,使用正则表达式方法提取. 当你想在HTML的img标签中提取图像数据,这非常有用. 如果你知道如何修改正则表达式,那么同样的功能进 ...

  7. http://my.oschina.net/pangyangyang/blog/144495

    http://my.oschina.net/pangyangyang/blog/144495

  8. 解决版本冲突-使用SVN主干与分支功能

    解决版本冲突-使用SVN主干与分支功能 1  前言 大多数产品开发存在这样一个生命周期:编码.测试.发布,然后不断重复.通常是这样的开发步骤: 1)    开发人员开发完毕某一版本(如版本A)功能后, ...

  9. SSIS ->> Logging

    SSIS提供了Event Handler之外的另一种方法捕捉Event和获取需要的信息,这种方法是Logging.SSIS的Logging针对不同的组件可以提供比Event Handler更多的Eve ...

  10. android ADB命令的使用

    http://jingyan.baidu.com/article/fcb5aff7f55c63edab4a7174.html 综上所述,我觉得告知各位菜鸟同学如何删除自带的程序是很有必要的一件事情.1 ...