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

 

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

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

即 file_exists = is_dir + is_file。

与 file_exists 相比,is_file 与 is_dir 的执行效率如何呢?

分别执行1000次,记录所需时间:

文件存在(当前目录)
is_file:0.4570ms
file_exists:2.0640ms

文件存在(绝对路径3层/www/hx/a/)
is_file:0.4909ms
file_exists:3.3500ms

文件存在(绝对路径5层/www/hx/a/b/c/)
is_file:0.4961ms
file_exists:4.2100ms

文件不存在(当前目录)
is_file:2.0170ms
file_exists:1.9848ms

文件不存在(绝对路径5层/www/hx/a/b/c/)
is_file:4.1909ms
file_exists:4.1502ms

目录存在
file_exists:2.9271ms
is_dir:0.4601ms
目录不存在
file_exists:2.9719ms
is_dir:2.9359ms

is_file($file),file_exists($file)

当$file是目录时,is_file返回false,file_exists返回true

文件存在的情况下,is_file比file_exists要快得多;
要检测文件所在的目录越深,速度差越多,但至少快4倍。

文件不存在的情况下,is_file比file_exists要慢一点点,但可以忽略不计。

目录存在的情况下,is_dir比file_exists要快得多;

目录不存在的情况下,is_dir比file_exists要慢一点点,但可以忽略不计。

结论:

如果要判断文件是否存在,用函数 is_file(),

如果要判断目录是否存在,用函数 is_dir(),

好像没地方需要用file_exists了,不确定传入的参数是文件还是目录的时候用?

--> 可以预估下,当文件/文件夹不存在的概念比较大时,就用 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
24
25
26
27
28
29
30
<?php
function runtime($t1)
{
    return number_format((microtime(true) - $t1) * 1000, 4) . 'ms';
}
  
$times = 1000;
$t1 = microtime(true);
for ($i = 0; $i $times$i++)
{
    is_file('/www/hx/blog.snsgou.com/config.php');
}
  
echo '<br>is_file:' . runtime($t1);
  
$t2 = microtime(true);
for ($i = 0; $i $times$i++)
{
    file_exists('/www/hx/blog.snsgou.com/config.php');
}
echo '<br>file_exists:' . runtime($t2);
  
/*
$t3 = microtime(true);
for ($i = 0; $i < $times; $i++)
{
    is_dir('/www/hx/blog.snsgou.com/');
}
echo '<br>is_dir:' . runtime($t3);
*/

PHP中file_exists与is_file、is_dir的区别,以及执行效率的比较 转自#冰雪傲骨#的更多相关文章

  1. 简单理解Struts2中拦截器与过滤器的区别及执行顺序

    简单理解Struts2中拦截器与过滤器的区别及执行顺序 当接收到一个httprequest , a) 当外部的httpservletrequest到来时 b) 初始到了servlet容器 传递给一个标 ...

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

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

  3. Select count(*)、Count(1)、Count(0)的区别和执行效率比较

    记得很早以前就有人跟我说过,在使用count的时候要用count(1)而不要用count(*),因为使用count(*)的时候会对所有的列进行扫描,相比而言count(1)不用扫描所有列,所以coun ...

  4. JQ中$(window).load和$(document).ready区别与执行顺序

    JQ中的$(document).ready()大家应该用的非常多,等同于$(function(){}),基本每个JS脚本中都有这个函数的出现有时甚至会出现多个,那么另一个加载函数$(window).l ...

  5. [转]JQ中$(window).load和$(document).ready区别与执行顺序

    一.$(window).load().window.onload=function(){}和$(document).ready()方法的区别 1.$(window).load() 和window.on ...

  6. 转: Struts2中拦截器与过滤器的区别及执行顺序

    当接收到一个httprequest , a) 当外部的httpservletrequest到来时 b) 初始到了servlet容器 传递给一个标准的过滤器链 c) FilterDispatecher会 ...

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

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

  8. JDBC中的Statement和PreparedStatement的区别

    JDBC中的Statement和PreparedStatement的区别  

  9. java中Map,List与Set的区别(转)

    Set,List,Map的区别 java集合的主要分为三种类型: Set(集) List(列表) Map(映射) 要深入理解集合首先要了解下我们熟悉的数组: 数组是大小固定的,并且同一个数组只能存放类 ...

随机推荐

  1. Keil中使用Astyel进行C语言的格式化

    Astyel !E --style=linux --delete-empty-lines --indent=spaces=2 --break-blocks 这可以做到, 使用Linux风格的代码 ) ...

  2. 大约php,mysql,html数字寻呼和文本分页2分页样式供大家参考

    做盗版.转载请添加源http://blog.csdn.net/yanlintao1 请勿盗版,转载请加上出处http://blog.csdn.net/yanlintao1 首先进行样式展示希望对大家有 ...

  3. javascript 学习总结(五)Function对象

    1.Function  函数调用(类似call方法) function callSomeFunction(someFunction, someArgument){ return someFunctio ...

  4. VC++注射过程

    2014/10/19 11:12 // stdafx.h : // // // #pragma once #include "targetver.h" #include <s ...

  5. SSIS Package to Call Web Service

    原文 SSIS Package to Call Web Service SSIS Package to Call Web Service. You can Call WebService from S ...

  6. OWIN产生的背景以及简单介绍

    OWIN产生的背景以及简单介绍 随着VS2013的发布,微软在Asp.Net中引入了很多新的特性,比如使用新的权限验证模块Identity, 使用Async来提高Web服务器的吞吐量和效率等.其中一个 ...

  7. leetcode[89] Merge Sorted Array

    合并两个有序数组,放在A中,A中的空间足够. Given two sorted integer arrays A and B, merge B into A as one sorted array. ...

  8. 查看mysql状态常用命令

    最近服务器上mysql有些奇奇怪怪的问题,可惜我不是专业的dba,为了加深自己对mysql的了解,先从基础的查看mysql状态命令看起吧. 命令: show status; 命令: show stat ...

  9. MVC 5 的 EF6 Code First 入门 系列:排序、筛选和分页

    这是微软官方SignalR 2.0教程Getting Started with Entity Framework 6 Code First using MVC 5 系列的翻译,这里是第三篇:排序.筛选 ...

  10. django restful webservice返回json数据

    做这个demo的前提是你已经配好了python ,django ,djangorestframwork(在我的上一篇博客中有介绍,大家也可以google),mysql-python等. djangor ...