Last active
August 18, 2019 00:07
-
-
Save calebporzio/52cc47ccd8ef75eff33cb45d56b4874b to your computer and use it in GitHub Desktop.
A simple helper function and macro for timing php scripts and eloquent queries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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