// yes, the argument list can be empty function foo() { // returns an array of all passed arguments $args = func_get_args(); foreach ($args as $k => $v) { echo "arg".($k+1).": $v\n"; } } foo(); /* prints nothing */ foo('hello'); /* prints arg1: hello */ foo('hello', 'world', 'again'); /* prints arg1: hello arg2: world arg3: again */
ru_oublock: block output operations ru_inblock: block input operations ru_msgsnd: messages sent ru_msgrcv: messages received ru_maxrss: maximum resident set size ru_ixrss: integral shared memory size ru_idrss: integral unshared data size ru_minflt: page reclaims ru_majflt: page faults ru_nsignals: signals received ru_nvcsw: voluntary context switches ru_nivcsw: involuntary context switches ru_nswap: swaps ru_utime.tv_usec: user time used (microseconds) ru_utime.tv_sec: user time used (seconds) ru_stime.tv_usec: system time used (microseconds) ru_stime.tv_sec: system time used (seconds)
// 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); /* prints User time: 0.011552 System time: 0 */
复制代码
虽然剧本运转用了大约 3 秒钟,CPU 利用率却十分十分低。由于在睡眠运转的过程当中,该剧本实践上不用耗 CPU 资本。还有很多其他的义务,能够需求一段工夫,但不占用相似守候磁盘操作等 CPU 工夫。因而正如你所看到的,CPU 利用率和运转工夫的实践长度其实不老是不异的。上面是一个例子:
// yes, the argument list can be empty function foo() { // returns an array of all passed arguments $args = func_get_args(); foreach ($args as $k => $v) { echo "arg".($k+1).": $v\n"; } } foo(); /* prints nothing */ foo('hello'); /* prints arg1: hello */ foo('hello', 'world', 'again'); /* prints arg1: hello arg2: world arg3: again */ 0
复制代码
这花了大约 1.4 秒的 CPU 工夫,但几近都是用户工夫,由于没有体系挪用。体系工夫是指消费在履行法式的体系挪用时的 CPU 开支。上面是一个例子:
// yes, the argument list can be empty function foo() { // returns an array of all passed arguments $args = func_get_args(); foreach ($args as $k => $v) { echo "arg".($k+1).": $v\n"; } } foo(); /* prints nothing */ foo('hello'); /* prints arg1: hello */ foo('hello', 'world', 'again'); /* prints arg1: hello arg2: world arg3: again */ 1
// yes, the argument list can be empty function foo() { // returns an array of all passed arguments $args = func_get_args(); foreach ($args as $k => $v) { echo "arg".($k+1).": $v\n"; } } foo(); /* prints nothing */ foo('hello'); /* prints arg1: hello */ foo('hello', 'world', 'again'); /* prints arg1: hello arg2: world arg3: again */ 2
复制代码
利用 __LINE__ 使得调试更加轻松。你可以跟踪到详细行号。
// yes, the argument list can be empty function foo() { // returns an array of all passed arguments $args = func_get_args(); foreach ($args as $k => $v) { echo "arg".($k+1).": $v\n"; } } foo(); /* prints nothing */ foo('hello'); /* prints arg1: hello */ foo('hello', 'world', 'again'); /* prints arg1: hello arg2: world arg3: again */ 3
// yes, the argument list can be empty function foo() { // returns an array of all passed arguments $args = func_get_args(); foreach ($args as $k => $v) { echo "arg".($k+1).": $v\n"; } } foo(); /* prints nothing */ foo('hello'); /* prints arg1: hello */ foo('hello', 'world', 'again'); /* prints arg1: hello arg2: world arg3: again */ 4
复制代码
There is actually a PHP function named uniqid() that is meant to be used for this.
// yes, the argument list can be empty function foo() { // returns an array of all passed arguments $args = func_get_args(); foreach ($args as $k => $v) { echo "arg".($k+1).": $v\n"; } } foo(); /* prints nothing */ foo('hello'); /* prints arg1: hello */ foo('hello', 'world', 'again'); /* prints arg1: hello arg2: world arg3: again */ 5
复制代码
你能够会注重到,虽然字符串是独一的,前几个字符倒是相似的,这是由于生成的字符串与办事器工夫相干。但实践上也存在友爱的一方面,因为每一个重生成的 ID 会按字母按次分列,如许排序就变得很复杂。为了削减反复的几率,你可以传递一个前缀,或第二个参数来增添熵:
// yes, the argument list can be empty function foo() { // returns an array of all passed arguments $args = func_get_args(); foreach ($args as $k => $v) { echo "arg".($k+1).": $v\n"; } } foo(); /* prints nothing */ foo('hello'); /* prints arg1: hello */ foo('hello', 'world', 'again'); /* prints arg1: hello arg2: world arg3: again */ 6
// yes, the argument list can be empty function foo() { // returns an array of all passed arguments $args = func_get_args(); foreach ($args as $k => $v) { echo "arg".($k+1).": $v\n"; } } foo(); /* prints nothing */ foo('hello'); /* prints arg1: hello */ foo('hello', 'world', 'again'); /* prints arg1: hello arg2: world arg3: again */ 7
// yes, the argument list can be empty function foo() { // returns an array of all passed arguments $args = func_get_args(); foreach ($args as $k => $v) { echo "arg".($k+1).": $v\n"; } } foo(); /* prints nothing */ foo('hello'); /* prints arg1: hello */ foo('hello', 'world', 'again'); /* prints arg1: hello arg2: world arg3: again */ 8
// yes, the argument list can be empty function foo() { // returns an array of all passed arguments $args = func_get_args(); foreach ($args as $k => $v) { echo "arg".($k+1).": $v\n"; } } foo(); /* prints nothing */ foo('hello'); /* prints arg1: hello */ foo('hello', 'world', 'again'); /* prints arg1: hello arg2: world arg3: again */ 9