is_file file_exists microtime performance

对项目中旧代码的疑问

1 } elseif (substr($class_name, 0, 6) == ‘OAuth2‘) {
2     $file_name = $C->INCPATH . ‘classes/oauth2/‘ . $class_name . ‘.php‘;
3     //为提升性能,不做判断,或者改成用is_file函数来判断
4     //if (file_exists($file_name)){
5     if (is_file($file_name)) {
6         require_once($file_name);
7         $found = TRUE;
8     }
9 }

自己验证的代码

 1 <?php
 2
 3
 4 $ws = microtime();
 5 //sleep(3);
 6 $we = microtime();
 7 var_dump($ws);
 8 var_dump($we);
 9 var_dump($we-$ws);
10
11 /*
12  * microtime()
13  * By default, microtime() returns a string in the form "msec sec",
14  * where sec is the number of seconds since the Unix epoch (0:00:00 January 1,1970 GMT),
15  * and msec measures microseconds that have elapsed since sec and is also expressed in seconds.
16  *
17  * Return current Unix timestamp with microseconds  e:string ‘0.22918500 1472290892‘
18  *
19  * microtime(true) TRUE, then microtime() returns a float,
20  * which represents the current time in seconds since the Unix epoch accurate to the nearest microsecond.
21  * e:float 1472290896.2304
22  * */
23 function microtime_float(){
24     list($usec, $sec) = explode(" ", microtime());
25     var_dump($usec);
26     var_dump($sec);
27     return ((float)$usec + (float)$sec);
28 }
29
30 $time_start = microtime_float();
31 //sleep(1);
32 //usleep(100);
33 $time_end = microtime_float();
34 var_dump($time_start);
35 var_dump($time_end);
36 $time = $time_end - $time_start ;
37 var_dump( ‘Did nothing in ‘.$time);
38
39 var_dump(microtime());
40 var_dump(microtime(true));
41
42 $time_start = microtime(TRUE);
43 //sleep(2);
44 //usleep(200);
45 $time_end = microtime(TRUE);
46 echo $time_end - $time_start;
47
48
49 $script_name = $_SERVER[‘SCRIPT_NAME‘];
50 $tmp_arr = explode(‘/‘,$script_name);
51 var_dump($tmp_arr);
52 $tmp_count = count($tmp_arr) - 1;
53 $file = $tmp_arr[$tmp_count];
54 echo $file;
55 echo ‘<br>‘;
56
57 $time_start = microtime(TRUE);
58 if(is_file($file)){
59     $time_end = microtime(TRUE);
60     echo $time_end - $time_start;
61 }
62 echo ‘<br>‘;
63 $time_start = microtime(TRUE);
64 if(file_exists($file)){
65     $time_end = microtime(TRUE);
66     echo $time_end - $time_start;
67 }
68 echo ‘<br>‘;
69
70 $time_start = microtime(TRUE)*1000*1000;
71 if(is_file($file)){
72     $time_end = microtime(TRUE)*1000*1000;
73     echo $time_end - $time_start;
74 }
75 echo ‘<br>‘;
76 $time_start = microtime(TRUE)*1000*1000;
77 if(file_exists($file)){
78     $time_end = microtime(TRUE)*1000*1000;
79     echo $time_end - $time_start;
80 }
81 echo ‘<br>‘;

执行结果

 1 D:\wamp64\www\w0827pm\is_file_file_exist.php:7:string ‘0.56736700 1472294444‘ (length=21)
 2
 3 D:\wamp64\www\w0827pm\is_file_file_exist.php:8:string ‘0.56736700 1472294444‘ (length=21)
 4
 5 D:\wamp64\www\w0827pm\is_file_file_exist.php:9:float 0
 6
 7 D:\wamp64\www\w0827pm\is_file_file_exist.php:25:string ‘0.56736700‘ (length=10)
 8
 9 D:\wamp64\www\w0827pm\is_file_file_exist.php:26:string ‘1472294444‘ (length=10)
10
11 D:\wamp64\www\w0827pm\is_file_file_exist.php:25:string ‘0.56736700‘ (length=10)
12
13 D:\wamp64\www\w0827pm\is_file_file_exist.php:26:string ‘1472294444‘ (length=10)
14
15 D:\wamp64\www\w0827pm\is_file_file_exist.php:34:float 1472294444.5674
16
17 D:\wamp64\www\w0827pm\is_file_file_exist.php:35:float 1472294444.5674
18
19 D:\wamp64\www\w0827pm\is_file_file_exist.php:37:string ‘Did nothing in 0‘ (length=16)
20
21 D:\wamp64\www\w0827pm\is_file_file_exist.php:39:string ‘0.56736700 1472294444‘ (length=21)
22
23 D:\wamp64\www\w0827pm\is_file_file_exist.php:40:float 1472294444.5674
24
25 0
26
27 D:\wamp64\www\w0827pm\is_file_file_exist.php:51:
28 array (size=3)
29   0 => string ‘‘ (length=0)
30   1 => string ‘w0827pm‘ (length=7)
31   2 => string ‘is_file_file_exist.php‘ (length=22)
32
33 is_file_file_exist.php
34 0
35 0
36 0
37 0

为什么时间差是0??时间差是绝对值。

http://stackoverflow.com/questions/792899/is-file-or-file-exists-in-php

修改代码

 1 $time_start = microtime(TRUE);
 2 for($w = 0; $w < 10000; $w++){
 3     is_file($file);
 4 }
 5 $time_end = microtime(TRUE);
 6 echo $time_end - $time_start ;
 7 echo ‘<br>‘;
 8 $time_start = microtime(TRUE);
 9 for($w = 0; $w < 10000; $w++){
10     file_exists($file);
11 }
12 $time_end = microtime(TRUE);
13 echo $time_end - $time_start ;

执行结果

1 0.060003042221069
2 0.22301292419434

再分析获得for循环启发的代码

时间: 2025-01-15 06:31:02

is_file file_exists microtime performance的相关文章

PHP加速插件-opcache参数详解

zend_extension=opcache.so    #这样去加扩展 opcache.memory_consumption=128      #为opcache分配多少共享内存128M opcache.interned_strings_buffer=8   #interned string的内存大小 opcache.max_accelerated_files=4000  #最大缓存的文件数目 opcache.revalidate_freq=300         #opcache自动检测文件

php.ini

1 [PHP] 2 3 ;;;;;;;;;;;;;;;;;;;; 4 ; php.ini Options ; 5 ;;;;;;;;;;;;;;;;;;;; 6 ; Name for user-defined php.ini (.htaccess) files. Default is ".user.ini" 7 ;user_ini.filename = ".user.ini" 8 9 ; To disable this feature set this option

A PHP log class for debuging

分享一个我自己用的在 WordPress 开发中用得到的一个调试日志类. <?php /**  * @author: suifengtec coolwp.com  * @date:   2013-02-03 09:55:55  * @last Modified by:   suifengtec coolwp.com  * @last Modified time: 2015-07-12 18:40:02  */ if(function_exists('add_action')){     defi

分享一个PHP调试日志类

分享一个我自己用的在 WordPress 开发中用得到的一个调试日志类. <?php /** * @author: suifengtec coolwp.com * @date: 2013-02-03 09:55:55 * @last Modified by: suifengtec coolwp.com * @last Modified time: 2015-07-12 18:40:02 */ if(function_exists('add_action')){ defined('ABSPATH'

常用php函数

1.数据打印echo, 可以同时输出多个字符串,多个参数,无返回值 print,只能输出一个字符串,一个参数 print_r,用于输出数组 die,先输出内容,在退出程序 var_dump 如果想查看某个 表达式的值和类型,用var_dump(),如果只是想得到一个表达式的类型,用gettype(),查看某个类型,用is_type(). 2.数据判断       (1). isset, 判断变量是否设置,只能用于变量 .若变量不存在则返回 FALSE ,若变量存在且其值为NULL,也返回 FAL

windows vs code 配置调试php

1.安装好vs code 2.安装好nginx 3.下载好php执行文件 php.ini [PHP] ;;;;;;;;;;;;;;;;;;; ; About php.ini ; ;;;;;;;;;;;;;;;;;;; ; PHP's initialization file, generally called php.ini, is responsible for ; configuring many of the aspects of PHP's behavior. ; PHP attempts

wampserver2.2e-php5.3.13 版本 增加 php7 支持

公司需要升级PHP版本至PHP7,对于用惯了wamp的来说,添加一下PHP扩展应该是很容易的,看我以前的文章(WampServer自己DIY添加apache.php.mysql版本). 不过再配置的当中并不顺利,要集成php7.1.15花了一点时间,因为使用的是 wampserver2.2e-php5.3.13-httpd2.2.22-mysql5.5.24-32b.exe 这个版本,导致集成出现两个问题: 1.php7版本提供的apache扩展dll文件并不支持apache2.2,只支持apa

opcache的配置

; Enable Zend OPcache extension module zend_extension=opcache.so ; Determines if Zend OPCache is enabled opcache.enable=1 ; Determines if Zend OPCache is enabled for the CLI version of PHP ;opcache.enable_cli=0 ; The OPcache shared memory storage siz

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.