Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jazzsequence/19b8e7308f388a9f8207 to your computer and use it in GitHub Desktop.
Save jazzsequence/19b8e7308f388a9f8207 to your computer and use it in GitHub Desktop.
<?php
/**
* Time a callable function/method
*
* Returns an array containing the response of the callback in the first index,
* and the total time for execution in the second key.
*
* @param mixed $callback A callable function/method (i.e. `get_posts` or `array( $my_obj, 'my_method' )`)
* @return mixed Array with func value response, and execution time
*/
function wds_time_it( $callback ) {
$args = func_get_args();
// Remove callback from args
unset( $args[ array_search( $callback, $args ) ] );
if ( ! function_exists( 'microtime' ) ) {
return array( call_user_func_array( $callback, $args ), false );
}
$time_start = microtime( true );
$data_to_return = call_user_func_array( $callback, $args );
$total_time = microtime( true ) - $time_start;
return array( $data_to_return, $total_time );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment