首页 > 代码库 > is_file file_exists microtime performance

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.230422  * */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.567416 17 D:\wamp64\www\w0827pm\is_file_file_exist.php:35:float 1472294444.567418 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.567424 25 026 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.php34 035 036 037 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.0600030422210692 0.22301292419434

 

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

 

is_file file_exists microtime performance