note 61786 added to function.microtime

From: Date: Sat, 11 Feb 2006 23:04:48 +0000
Subject: note 61786 added to function.microtime
Groups: php.notes 
Request: Send a blank email to php-notes+get-103534@lists.php.net to get a copy of this message
The following is simple general purpose profiling module you can use to easily profile arbitrary blocks of code. It will let you name and log any block of code, and then will track the total execution time of the block, the number of times it was called, and the average execution time per call. Usage: <?php ProfileStart( "outer block" ); sleep( 1000 ); // do other stuff here for( $i = 0; $i < 100; $i++ ) { ProfileStart( "inner block" ); sleep( 100 ); // do more stuff here ProfileEnd( "inner block" ); } ProfileEnd( "outer block" ); ProfileEchoLog(); ?> Here's the code itself: <?php $gProfileLogs = array(); $gProfileTimers = array(); function ProfileGetTime() { list( $uSec, $sec ) = explode( " ", microtime() ); return( (float)$uSec + (float)$sec ); } function ProfileStart( $log ) { global $gProfileTimers; $gProfileTimers[ $log ] = ProfileGetTime(); } function ProfileEnd( $log ) { global $gProfileTimers; global $gProfileLogs; $elapsed = ProfileGetTime() - $gProfileTimers[ $log ]; if( array_key_exists( $log, $gProfileLogs ) ) { $gProfileLogs[ $log ][ "elapsed" ] += $elapsed; $gProfileLogs[ $log ][ "count" ] ++; } else { $record = array(); $record[ "elapsed" ] = $elapsed; $record[ "count" ] = 1; $gProfileLogs[ $log ] = $record; } } function ProfileEchoLog() { global $gProfileLogs; echo "<table>\n"; echo "<tr><td>log</td><td>elapsed</td>"; echo "<td>count</td><td>average</td></tr>\n"; foreach( array_keys( $gProfileLogs ) as $log ) { $elapsed = $gProfileLogs[ $log ][ "elapsed" ]; $count = $gProfileLogs[ $log ][ "count" ]; $average = $elapsed / $count; $format = "<tr><td>%s</td><td>%.4f</td>"; $format .= "<td>%d</td><td>%.4f</td></tr>\n"; printf( $format, $log, $elapsed, $count, $average ); } echo "</table>\n"; } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 65.33.160.120 ---- Manual Page -- http://www.php.net/manual/en/function.microtime.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+61786 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61786&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61786&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61786&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61786&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61786&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61786&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61786&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+61786&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#103534) next »