// sleep for 3 seconds (non-busy) sleep(3); $data = getrusage(); echo \"User time: \". ($data[\'ru_utime.tv_sec\'] + $data[\'ru_utime.tv_usec\'] / 1000000); echo \"System time: \". ($data[\'ru_stime.tv_sec\'] + $data[\'ru_stime.tv_usec\'] / 1000000); /* 输出 User time: 0.011552 System time: 0 */ |
// loop 10 million times (busy) for($i=0;$i hello [1] => 42 [2] => Array ( [0] => 1 [1] => two ) [3] => apple ) */ |
// a complex array $myvar = array( \'hello\', 42, array(1,\'two\'), \'apple\' ); // convert to a string $string = json_encode($myvar); echo $string; /* prints [\"hello\",42,[1,\"two\"],\"apple\"] */ // you can reproduce the original variable $newvar = json_decode($string); print_r($newvar); /* prints Array ( [0] => hello [1] => 42 [2] => Array ( [0] => 1 [1] => two ) [3] => apple ) */ |
$string = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc ut elit id mi ultricies adipiscing. Nulla facilisi. Praesent pulvinar, sapien vel feugiat vestibulum, nulla dui pretium orci, non ultricies elit lacus quis ante. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam pretium ullamcorper urna quis iaculis. Etiam ac massa sed turpis tempor luctus. Curabitur sed nibh eu elit mollis congue. Praesent ipsum diam, consectetur vitae ornare a, aliquam a nunc. In id magna pellentesque tellus posuere adipiscing. Sed non mi metus, at lacinia augue. Sed magna nisi, ornare in mollis in, mollis sed nunc. Etiam at justo in leo congue mollis. Nullam in neque eget metus hendrerit scelerisque eu non enim. Ut malesuada lacus eu nulla bibendum id euismod urna sodales. \"; $compressed = gzcompress($string); echo \"Original size: \". strlen($string).\"n\"; /* 输出原始大小 Original size: 800 */ echo \"Compressed size: \". strlen($compressed).\"n\"; /* 输出压缩后的大小 Compressed size: 418 */ // 解压缩 $original = gzuncompress($compressed); |
// capture the start time $start_time = microtime(true); // do some stuff // ... // display how long the script took echo \"execution took: \". (microtime(true) - $start_time). \" seconds.\"; |
$start_time = microtime(true); register_shutdown_function(\'my_shutdown\'); // do some stuff // ... function my_shutdown() { global $start_time; echo \"execution took: \". (microtime(true) - $start_time). \" seconds.\"; } |
// 两个默认参数的函数 function foo($arg1 = \'\', $arg2 = \'\') { echo \"arg1: $arg1n\"; echo \"arg2: $arg2n\"; } foo(\'hello\',\'world\'); /* 输出: arg1: hello arg2: world */ foo(); /* 输出: arg1: arg2: */ |
// 是的,形参列表为空 function foo() { // 取得所有的传入参数的数组 $args = func_get_args(); foreach ($args as $k => $v) { echo \"arg\".($k+1).\": $vn\"; } } foo(); /* 什么也不会输出 */ foo(\'hello\'); /* 输出 arg1: hello */ foo(\'hello\', \'world\', \'again\'); /* 输出 arg1: hello arg2: world arg3: again */ |
// 取得所有的后缀为PHP的文件 $files = glob(\'*.php\'); print_r($files); /* 输出: Array ( [0] => phptest.php [1] => pi.php [2] => post_output.php [3] => test.php ) */ |
// 取PHP文件和TXT文件 $files = glob(\'*.{php,txt}\', GLOB_BRACE); print_r($files); /* 输出: Array ( [0] => phptest.php [1] => pi.php [2] => post_output.php [3] => test.php [4] => log.txt [5] => test.txt ) */ 你还可以加上路径: $files = glob(\'../images/a*.jpg\'); print_r($files); /* 输出: Array ( [0] => ../images/apple.jpg [1] => ../images/art.jpg ) */ |
$files = glob(\'../images/a*.jpg\'); // applies the function to each array element $files = array_map(\'realpath\',$files); print_r($files); /* output looks like: Array ( [0] => C:wampwwwimagesapple.jpg [1] => C:wampwwwimagesart.jpg ) */ |
echo \"Initial: \".memory_get_usage().\" bytes n\"; /* 输出 Initial: 361400 bytes */ // 使用内存 for ($i = 0; $i < 100000; $i++) { $array []= md5($i); } // 删除一半的内存 for ($i = 0; $i < 100000; $i++) { unset($array[$i]); } echo \"Final: \".memory_get_usage().\" bytes n\"; /* prints Final: 885912 bytes */ echo \"Peak: \".memory_get_peak_usage().\" bytes n\"; /* 输出峰值 Peak: 13687072 bytes */ |
print_r(getrusage()); /* 输出 Array ( [ru_oublock] => 0 [ru_inblock] => 0 [ru_msgsnd] => 2 [ru_msgrcv] => 3 [ru_maxrss] => 12692 [ru_ixrss] => 764 [ru_idrss] => 3864 [ru_minflt] => 94 [ru_majflt] => 0 [ru_nsignals] => 1 [ru_nvcsw] => 67 [ru_nivcsw] => 4 [ru_nswap] => 0 [ru_utime.tv_usec] => 0 [ru_utime.tv_sec] => 0 [ru_stime.tv_usec] => 6269 [ru_stime.tv_sec] => 0 ) */ |
ru_oublock: 块输出操作 ru_inblock: 块输入操作 ru_msgsnd: 发送的message ru_msgrcv: 收到的message ru_maxrss: 最大驻留集大小 ru_ixrss: 全部共享内存大小 ru_idrss:全部非共享内存大小 ru_minflt: 页回收 ru_majflt: 页失效 ru_nsignals: 收到的信号 ru_nvcsw: 主动上下文切换 ru_nivcsw: 被动上下文切换 ru_nswap: 交换区 ru_utime.tv_usec: 用户态时间 (microseconds) ru_utime.tv_sec: 用户态时间(seconds) ru_stime.tv_usec: 系统内核时间 (microseconds) ru_stime.tv_sec: 系统内核时间?(seconds) |
欢迎光临 创星网络[分享知识 传递快乐] (http://bbs.cxweb.com.cn/) | Powered by Discuz! X3 |