Skip to content

Instantly share code, notes, and snippets.

@mogetutu
Forked from calebporzio/timer_helpers.php
Created July 31, 2017 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mogetutu/11fe88ec6c723ec71483258167571a4a to your computer and use it in GitHub Desktop.
Save mogetutu/11fe88ec6c723ec71483258167571a4a to your computer and use it in GitHub Desktop.
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