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() 既可用于判断文件 ...
随机推荐
- Ajax实例-购物车
一.概述 1.当添加或删除商品时,购物车会立即更新数据 2.思路: (1)建立商品类Item.java,存有商品属性name,prince,code(商品编码)等 (2)建立商品目录类Catalog. ...
- Android笔记——RecyclerView替代ListView
ListView是常用列表控件,但设置Adapter时自定义代码较为复杂,因此Android3.0后,增加RecyclerView替代ListView RecyclerView没有提供OnItemCl ...
- SQLServer错误代码解释
SQLServer出现错误的代码大全(好用) Code Error Message 0 操作成功完成. 1 功能错误. 2 系统找不到指定的文件. 3 系统找不到指定的路径. 4 系统无法打开文件. ...
- Android 常用时间格式转换代码
/** * 获取现在时间 * * @return 返回时间类型 yyyy-MM-dd HH:mm:ss */ public static Date getNowDate() { Date curren ...
- SSIS ->> Script Debugging and Troubleshooting
Breakpoint是调试过程中最重要的手段,不仅对于Script Task和Script Component,对于任何其他的组件也是如此.可以在某个Event(如OnError)触发的时候设置断点来 ...
- USACO Section 2.4: The Tamworth Two
这题我是用蒙的方法来弄出最后的不能碰到的条件的(用1000试了下account跳出条件),结果竟然还过了,不过网上有精准的求出这个碰不到的条件,farm的状态为10*10*4 = 400,cow的状态 ...
- HDU 4634 Swipe Bo 状态压缩+BFS最短路
将起始点.终点和钥匙统一编号,预处理: 1.起始点到所有钥匙+终点的最短路 2.所有钥匙之间两两的最短路 3.所有钥匙到终点的最短路 将起始点和所有钥匙四方向出发设为起点BFS一遍,求出它到任意点任意 ...
- Node 实现 AES 加密,结果输出为“byte”。
Node 实现 AES 加密,结果输出为"byte". 最近做个需求,对接一个平台的接口,该平台采用 AES (Advanced Encryption Standard)加密算法, ...
- git忽略文件【转】
转自: http://cwind.iteye.com/blog/1666646 有很多文件不必使用git管理.例如Eclipse或其他IDE生成的项目文件,编译生成的各种目标或临时文件等.使用git ...
- Git - Tutorial官方【转】
转自:http://www.vogella.com/tutorials/Git/article.html#git_rename_branch Lars Vogel Version 5.8 Copyri ...