Posted on March 4, 2015
Handy function that calculates your function/method execution time so you can optimize your code’s performance.
/** * 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 dw_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 ); }
Recent Comments