Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple helper function and macro for timing php scripts and eloquent queries
<?php
// Helper function.
if (! function_exists('timer')) {
function timer($expression)
{
$start = microtime(true);
if ($expression instanceof Closure) {
$expression();
} else {
eval(rtrim($expression, ';') . ';');
}
return microtime(true) - $start . " Seconds";
}
}
// Query builder macro.
Illuminate\Database\Eloquent\Builder::macro('timer', function() {
$start = microtime(true);
$this->get();
return microtime(true) - $start . " Seconds";
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment