is_file和file_exists效率比较
目前在弄文件缓存的时候用到了判定文件存在与否,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效率比较的更多相关文章
- 黄聪:is_file和file_exists效率比较
目前在弄文件缓存的时候用到了判定文件存在与否,is_file()还是file_exists()呢?is_file和file_exists两者效率比较起来,谁的运行速度更快呢?还是做个测试吧: 1 2 ...
- is_file,is_dir,file_exists
is_file()和file_exists()效率比较,结果当文件存在时,is_file函数比file_exists函数速度快14倍,当文件不存在时,两者速度相当.同理,当文件目录存在时,is_dir ...
- PHP中file_exists与is_file、is_dir的区别,以及执行效率的比较 转自#冰雪傲骨#
PHP中file_exists与is_file.is_dir的区别,以及执行效率的比较 判断文件是否存在,有2个常用的PHP函数:is_file 和 file_exists, 判断文件夹是否存在, ...
- PHP中file_exists与is_file、is_dir的区别,以及执行效率的比较
判断文件是否存在,有2个常用的PHP函数:is_file 和 file_exists, 判断文件夹是否存在,有2个常用PHP函数:is_dir 和 file_exists, 即 file_exists ...
- php判断文件存在是用file_exists 还是 is_file
From: http://www.php100.com/html/php/hanshu/2013/0905/4672.html [导读] 在写程序时发现在判断文件是否存在时,有两种写法,有的人用了is ...
- php 使用 file_exists 还是 is_file
Jesns 提出 file_exists 比较老了,建议使用 is_file 来判断文件. 经过我的测试,is_file 果然快很多,以后可以改 is_file 来判断文件. 还有相关链接: is_f ...
- php file_exists无效解决办法
一:is_file 和 file_exists 的区别:当文件存在时:is_file 比 file_exists快了N倍当文件不存在时:is_file 比 file_exists慢总之一句话:file ...
- php 文件下载
public function down() { header("Content-type:text/html;charset=utf-8"); $file_name = I('g ...
- php 判断文件/目录是否存的方法
涉及函数 is_file(), is_dir() , file_exists() is_file() 判断文件是否存在 is_dir() 判断目录是否存在 file_exists() 既可用于判断文件 ...
随机推荐
- spring_150803_component
实体类: package com.spring.model; public class DogPet { private int id; private String name; private in ...
- PLSQL:plsql中文乱码,显示问号
PLSQL执行sql语句,不识别中文,输出的中文标题显示成问号???? 1. 登陆plsql,执行sql语句,输出的中文标题显示成问号????:条件包含中文,则无数据输出
- hdu2025查找最大元素
#include<iostream> #include<stdio.h> #include<math.h> #include<stdlib.h> #in ...
- key-value数据库
http://blog.csdn.net/byane/article/details/6928519 传统的文件系统中,需要维护目录的层次结构,使用dentry,inode,directory等复杂结 ...
- 客户端用httpurlconnection来进行http连接的
客户端用httpurlconnection来进行http连接的,并设置restful风格 请求响应流程 设置连接参数的方法 setAllowUserInteraction setDoInput set ...
- iOS复杂动画之抽丝剥茧(Objective-C & Swift)
一.前言 随着开发者的增多和时间的累积,AppStore已经有非常多的应用了,每年都有很多新的APP产生.但是我们手机上留存的应用有限,所以如何吸引用户,成为产品设计的一项重要内容.其中炫酷的动画效果 ...
- Qt中的键盘事件,以及焦点的设置(比较详细)
Qt键盘事件属于Qt事件系统,所以事件系统中所有规则对按键事件都有效.下面关注点在按键特有的部分: focus 一个拥有焦点(focus)的QWidget才可以接受键盘事件.有输入焦点的窗口是活动窗口 ...
- 《mysql悲观锁总结和实践》-悲观锁
最近学习了一下数据库的悲观锁和乐观锁,根据自己的理解和网上参考资料总结如下: 悲观锁介绍(百科): 悲观锁,正如其名,它指的是对数据被外界(包括本系统当前的其他事务,以及来自外部系统的事务处理)修改持 ...
- AdventureWorksDW2008R2 attach: Unable to open the physical file. Operating system error 5: "5(Access is denied.)
http://stackoverflow.com/questions/26014133/adventureworksdw2008r2-attach-unable-to-open-the-physica ...
- extern的意思
还是在重温C++,看到extern的意思,觉得很有必要记下来. extern是用来干什么的大家想必都是很明白的. 如果有个全局变量,或者其他文件的全局变量,要在本文件或函数中使用,可以通过extern ...