is_file file_exists microtime performance
Posted rsapaper
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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循环启发的代码
以上是关于is_file file_exists microtime performance的主要内容,如果未能解决你的问题,请参考以下文章
is_file file_exists microtime performance
php学习之道:php中is_file和file_exist的差别,and推断文件夹is_dir