note 70787 added to function.microtime

From: Date: Fri, 27 Oct 2006 21:29:35 +0000
Subject: note 70787 added to function.microtime
Groups: php.notes 
Request: Send a blank email to php-notes+get-119194@lists.php.net to get a copy of this message
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). ---- Server IP: 83.138.144.80 Probable Submitter: 86.135.108.113 ---- Manual Page -- http://www.php.net/manual/en/function.microtime.php Edit -- https://master.php.net/note/edit/70787 Del: integrated -- https://master.php.net/note/delete/70787/integrated Del: useless -- https://master.php.net/note/delete/70787/useless Del: bad code -- https://master.php.net/note/delete/70787/bad+code Del: spam -- https://master.php.net/note/delete/70787/spam Del: non-english -- https://master.php.net/note/delete/70787/non-english Del: in docs -- https://master.php.net/note/delete/70787/in+docs Del: other reasons-- https://master.php.net/note/delete/70787 Reject -- https://master.php.net/note/reject/70787 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119194) next »