note 70787 deleted from function.microtime by nlopess
| From: | nlopess@php.net | Date: | Sat, 28 Oct 2006 10:43:34 +0000 |
| Subject: | note 70787 deleted from function.microtime by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-119208@lists.php.net to get a copy of this message | ||
Note Submitter: thomasREMOVETHIS at tgohome dot com
Reason: useless
----
I wrote this script because I wanted to monitor how fast my server is. It returns the amount of
iterations of a for-loop it's done in X seconds. This is not totally accurate because the
function initialisation takes a few microseconds, but it's a good base to see how well your
server is doing. My 1.79 ghz / 512 mb ram gets 63805 iterations in a second.
int server_get_speed( [float max_time [, int precision]] )
This function requires no arguments, but can take two optional ones. max_time defaults to 1.00
seconds and is a float. The loop is stopped when the max_time is reached. The second optional
argument is the precision. If you place 9 precision, the result will be very exact. This defaults to
4. Use caution, this script WILL slow the server until it's finished it's job.
<?
# How fast is your server? This will loop until a second
# has passed in time and return the iterations it did in
# that period. Good for monitoring how fast your server is.
# -----------------------------------------
# By: Thomas Oldbury (http://www.tgohome.com)
# PHP 4 required
function server_get_speed($max_time = 1.00, $precision = 4) {
// start the timer
list($usec, $sec) = explode(" ", microtime(true));
$start = (float)$usec + (float)$sec;
// check the value of the max time
if(!is_numeric($max_time)) {
// issue a warning
trigger_error(__FUNCTION__."(): Expecting parameter 1 (max time) of
".__FUNCTION__." to be an integer or float. will continue with default value",
E_USER_WARNING);
// set max time
$max_time = 1.00;
}
// check the value of the precision
if(!is_numeric($max_time)) {
// issue a warning
trigger_error(__FUNCTION__."(): Expecting parameter 2 (measurement precision) of
".__FUNCTION__." to be an integer, will continue with default value",
E_USER_WARNING);
// set precision
$precision = 4;
}
// iterate until the end of the earth
for($i = 1; $i > 0; $i++) {
// get the timer value
list($usec, $sec) = explode(" ", microtime(true));
$stop = (float)$usec + (float)$sec;
// is the time more than the max time?
if(round($stop - $start, $precision) >= $max_time) {
// return the $i value
return $i;
} else {
// continue, continue
continue;
}
}
}
# Examples:
echo server_get_speed() . " iterations of a for-loop in 1 second. <br>";
echo server_get_speed(2.50) . " iterations of a for-loop in 2.5 seconds. <br>";
echo server_get_speed(3, 9) . " iterations of a for-loop in 3 seconds (extra precise mode).
<br>";
?>
Example result (varies from server to server):
63805 iterations of a for-loop in 1 second.
164080 iterations of a for-loop in 2.5 seconds.
195638 iterations of a for-loop in 3 seconds (extra precise mode).