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

文章乃参考、转载其他博客所得,仅供自己学习作笔记使用!!!
 
转自:http://www.cnblogs.com/xuan52rock/p/4548635.html

黄聪: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. 黄聪:《跟黄聪学WordPress插件开发》

    续<跟黄聪学WordPress主题开发>之后,又一个作品完成!<跟黄聪学Wordpress插件开发>,国内最好的Wordpress插件开发视频教程!! 目录预览: WordPr ...

  4. 黄聪:PHP使用Simple_HTML_DOM遍历、过滤及保留指定属性

    <? /* * 参考资料: * http://www.phpddt.com/manual/simplehtmldom_1_5/manual_api.htm * http://www.phpddt ...

  5. 黄聪:《跟黄聪学WordPress主题开发》

    又一个作品完成!<跟黄聪学Wordpress主题开发>,国内最好的Wordpress主题模版开发视频教程!! 目录预览: WordPress官方源文件层式结构讲解 WordPress数据库 ...

  6. 黄聪:VS2010开发如何在c#中使用Ctrl、Alt、Tab等全局组合快捷键

    1.新建一个类 HotkeyHelper  using System; using System.Runtime.InteropServices; using System.Windows.Forms ...

  7. 黄聪:如何使用CodeSmith批量生成代码(转:http://www.cnblogs.com/huangcong/archive/2010/06/14/1758201.html)

    先看看CodeSmith的工作原理: 简单的说:CodeSmith首先会去数据库获取数据库的结构,如各个表的名称,表的字段,表间的关系等等,之后再根据用户自定义好的模板文件,用数据库结构中的关键字替代 ...

  8. 黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block 开发人员经常编写需要安全功能的应用程序.这些应用程序 ...

  9. 黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block 使用企业库异常处理应用程序模块的 ...

随机推荐

  1. sizeof和strlen区别(转)

    char str[20]="0123456789"; int   a=strlen(str); /*a=10;strlen 计算字符串的长度,以\0'为字符串结束标记. int   ...

  2. Android动态添加Device Admin权限

    /********************************************************************** * Android动态添加Device Admin权限 ...

  3. CodeMirror tab转空格

    解决CodeMirror编辑器Tab转空格问题 editor.setOption("extraKeys", { Tab: newTab }); function newTab(cm ...

  4. [LeetCode&Python] Problem 541. Reverse String II

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

  5. RuntimeError: Broken toolchain: cannot link a simple C program

    今天Python2.7下安装numpy的时候遇到了此错误,进过查阅发现以下方法可行 在Python27\Lib\distutils\msvc9compiler.py 文件中,找到 mfinfo = s ...

  6. Linux中查看显卡硬件信息

    Linux中查看显卡硬件信息 https://ywnz.com/linuxjc/67.html lspci -vnn | grep VGA -A 12lshw -C display 查看当前使用的显卡 ...

  7. NIO 多人聊天室

    一前言 在家休息没事,敲敲代码,用NIO写个简易的仿真聊天室.下面直接讲聊天室设计和编码.对NIO不了解的朋友,推荐一个博客,里面写的很棒: https://javadoop.com/     里面有 ...

  8. java中类与方法叙述正确的是

    这道题的4个选项全是错的.  A.一个文件中,可以有多个public class  public class Main { public class Inner{ } } 即,外部类为public,还 ...

  9. set集合的遍历(基于迭代器和增强for循环,没有一般的for循环)

    赋:开发项目中见到的代码 Java中Set集合是一个不包含重复元素的Collection,首先我们先看看遍历方法 package com.sort; import java.util.HashSet; ...

  10. Java打印水仙花数

    public class Test2 { public static void main(String[] args) { //水仙花 数 指的是一个三位数(100-999) //三位数本身= 百位数 ...