note 61786 deleted from function.microtime by aidan

From: Date: Sun, 12 Feb 2006 08:51:25 +0000
Subject: note 61786 deleted from function.microtime by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-103553@lists.php.net to get a copy of this message
Note Submitter: bob at robot-frog dot com Reason: useless example ---- 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"; } ?>

« previous php.notes (#103553) next »