黄聪: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 ...
- 黄聪:《跟黄聪学WordPress插件开发》
续<跟黄聪学WordPress主题开发>之后,又一个作品完成!<跟黄聪学Wordpress插件开发>,国内最好的Wordpress插件开发视频教程!! 目录预览: WordPr ...
- 黄聪:PHP使用Simple_HTML_DOM遍历、过滤及保留指定属性
<? /* * 参考资料: * http://www.phpddt.com/manual/simplehtmldom_1_5/manual_api.htm * http://www.phpddt ...
- 黄聪:《跟黄聪学WordPress主题开发》
又一个作品完成!<跟黄聪学Wordpress主题开发>,国内最好的Wordpress主题模版开发视频教程!! 目录预览: WordPress官方源文件层式结构讲解 WordPress数据库 ...
- 黄聪:VS2010开发如何在c#中使用Ctrl、Alt、Tab等全局组合快捷键
1.新建一个类 HotkeyHelper using System; using System.Runtime.InteropServices; using System.Windows.Forms ...
- 黄聪:如何使用CodeSmith批量生成代码(转:http://www.cnblogs.com/huangcong/archive/2010/06/14/1758201.html)
先看看CodeSmith的工作原理: 简单的说:CodeSmith首先会去数据库获取数据库的结构,如各个表的名称,表的字段,表间的关系等等,之后再根据用户自定义好的模板文件,用数据库结构中的关键字替代 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block 开发人员经常编写需要安全功能的应用程序.这些应用程序 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(七) Exception Handling Application Block 使用企业库异常处理应用程序模块的 ...
随机推荐
- mAP的计算
参加郑良博士的代码: mars_evaluation 其中ap这样算: ap = ap + (recall - old_recall)*((old_precision+precision)/2); ...
- pprof进行golang程序性能分析
一.导入包 import _ "net/http/pprof" 二.启动监控routine go func() { http.ListenAndServe("0.0.0. ...
- DG增量恢复
本篇文档: 讲述DG环境出现GAP归档缝隙,且主库的该归档日志被删除的情况下,快速恢复DG的连通性 流程讲述: >明确主库增量备份起点 scn 查询备库控制文件current scn ,及v$d ...
- Linux矫正时间
ntpdate -u ntp.api.bz 可以写到定时任务里,每天矫正一次
- SQLAlchemy中表结构的一对多
from flask import Flask from flask_sqlalchemy import SQLAlchemy import pymysql pymysql.install_as_My ...
- RNN-LSTM-GRU-BIRNN
https://blog.csdn.net/wangyangzhizhou/article/details/76651116 共三篇 RNN的模型展开后多个时刻隐层互相连接,而所有循环神经网络都有一个 ...
- 1001.A+B Format (20)题目解答
前言 最开始看到这个题目,我的第一个想法是有没有那种输出格式可以直接拿来用的,然后我百度了一下,想偷懒,然而并没有这种东西.只好动动自己的脑子了. 关于GitHub 这个问题,当初我弄了五天才建立好联 ...
- 2017.7.11 fuse工作原理
FUSE的工作原理如图所示.假设基于FUSE的用户态文件系统hello挂载在/tmp/fuse目录下.当应用层程序要访问/tmp/fuse下的文件时,通过glibc中的函数进行系统调用,处理这些系统调 ...
- mysql 查询进程和关闭进程
1.查询某一进程PID号 tasklist |findstr mysqld 2.关闭某一进程 taskkill /F /PID XXXX 3.制作 windows + r键输入services.msc ...
- hdu2732 Leapin' Lizards 最大流+拆点
Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As ...