PHP php的基准测试功能

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP php的基准测试功能相关的知识,希望对你有一定的参考价值。

<?php
/*
* Function: benchmark
* Author: Michael Bailey <mpbailey@byu.edu>
* Date: 5 Dec 2002
* License: GPL (General Public License)
* Purpose: This function will run the line of code
* sent as the first parameter and return
* the time it took to complete execution.
* If a second parameter is passed, the
* function will run the code the specified
* number of times, taking the average. This
* will return a better approximation. The
* last parameter allows you to send code to
* be executed between iterations, in case
* something needs to be reset (ie. global
* variables, etc...). The function returns
* the number of seconds
* with a bunch of decimal places.
*/
function benchmark($code, $iter = 1, $reset_code = null)
{
# Determine the overhead in calling the eval function
# You might want to to this multiple times to get better
# precision.
$start = microtime();
eval("");
$end = microtime();
list($start_ms, $start_s) = explode(" ", $start);
list($end_ms, $end_s) = explode(" ", $end);
$overhead = ($end_ms+$end_s) - ($start_ms+$start_s);

# Execute the code the specified number of times
for ($i=0; $i<$iter; $i++) {
$start = microtime();
eval($code);
$end = microtime();
list($start_ms, $start_s) = explode(" ", $start);
list($end_ms, $end_s) = explode(" ", $end);
$time += ($end_ms+$end_s) - ($start_ms+$start_s);

# If reset code was specified, evaluate it
if ($reset_code != null) eval($reset_code);
}

# Return the average speed subtracted by
# the overhead of calling eval()
return ($time/$iter) - $overhead;
}
?>

以上是关于PHP php的基准测试功能的主要内容,如果未能解决你的问题,请参考以下文章

PHP PHP简单的基准测试

PHP 使用PHP进行基准测试

PHP简单基准测试

PHP JavaScript / jQuery简单基准测试

PHP Framework MVC Benchmark 基准测试

SQL 基准测试:PHP ActiveRecord ORM 与 MySQL 与 CodeIgniter Active Record 与标准 PHP