九个PHP很有用的功能(2)
来源:http://coolshell.cn/?p=23944.
CPU使用信息
使用?getrusage()
函数可以让你知道CPU的使用情况。注意,这个功能在Windows下不可用。
print_r(getrusage());
/* 输出
Array
(
=> 0
=> 0
=> 2
=> 3
=> 12692
=> 764
=> 3864
=> 94
=> 0
=> 1
=> 67
=> 4
=> 0
=> 0
=> 0
=> 6269
=> 0
)
*/
这个结构看上出很晦涩,除非你对CPU很了解。下面一些解释:
[*]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)
要看到你的脚本消耗了多少CPU,我们需要看看“用户态的时间”和“系统内核时间”的值。秒和微秒部分是分别提供的,您可以把微秒值除以100万,并把它添加到秒的值后,可以得到有小数部分的秒数。
// 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
*/
sleep是不占用系统时间的,我们可以来看下面的一个例子:
// loop 10 million times (busy)
for($i=0;$i<10000000;$i++) {
}
$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: 1.424592
System time: 0.004204
*/
这花了大约14秒的CPU时间,几乎所有的都是用户的时间,因为没有系统调用。
系统时间是CPU花费在系统调用上的上执行内核指令的时间。下面是一个例子:
$start = microtime(true);
// keep calling microtime for about 3 seconds
while(microtime(true) - $start < 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);
/* prints
User time: 1.088171
System time: 1.675315
*/
我们可以看到上面这个例子更耗CPU。
5.
系统常量
PHP 提供非常有用的系统常量
可以让你得到当前的行号 (__LINE__),文件 (__FILE__),目录 (__DIR__),函数名 (__FUNCTION__),类名(__CLASS__),方法名(__METHOD__) 和名字空间 (__NAMESPACE__),很像C语言。
我们可以以为这些东西主要是用于调试,当也不一定,比如我们可以在include其它文件的时候使用?__FILE__ (当然,你也可以在 PHP 5.3以后使用 __DIR__ ),下面是一个例子。
// this is relative to the loaded script's path
// it may cause problems when running scripts from different directories
require_once('config/database.php');
// this is always relative to this file's path
// no matter where it was included from
require_once(dirname(__FILE__) . '/config/database.php');
下面是使用 __LINE__ 来输出一些debug的信息,这样有助于你调试程序:
// some code
// ...
my_debug("some debug message", __LINE__);
/* 输出
Line 4: some debug message
*/
// some more code
// ...
my_debug("another debug message", __LINE__);
/* 输出
Line 11: another debug message
*/
function my_debug($msg, $line) {
echo "Line $line: $msg\n";
}
6.生成唯一的ID
有很多人使用 md5() 来生成一个唯一的ID,如下所示:
// generate unique string
echo md5(time() . mt_rand(1,1000000));
其实,PHP中有一个叫?uniqid()
的函数是专门用来干这个的:
// generate unique string
echo uniqid();
/* 输出
4bd67c947233e
*/
// generate another unique string
echo uniqid();
/* 输出
4bd67c9472340
*/
可能你会注意到生成出来的ID前几位是一样的,这是因为生成器依赖于系统的时间,这其实是一个非常不错的功能,因为你是很容易为你的这些ID排序的。这点MD5是做不到的。
你还可以加上前缀避免重名:
// 前缀
echo uniqid('foo_');
/* 输出
foo_4bd67d6cd8b8f
*/
// 有更多的熵
echo uniqid('',true);
/* 输出
4bd67d6cd8b926.12135106
*/
// 都有
echo uniqid('bar_',true);
/* 输出
bar_4bd67da367b650.43684647
*/
而且,生成出来的ID会比MD5生成的要短,这会让你节省很多空间。
页:
[1]