Created
March 1, 2018 02:43
-
-
Save kicken/51c69b70fc8bd73e6a0fcc659641cd3b to your computer and use it in GitHub Desktop.
Time logging
This file contains 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 | |
function markTime($label){ | |
static $points = []; | |
static $firstRun = true; | |
if ($firstRun){ | |
$firstRun = false; | |
register_shutdown_function(function () use (&$points){ | |
$firstPoint = reset($points); | |
$lastPoint = end($points); | |
$firstTime = $firstPoint[1]; | |
$lastTime = $lastPoint[1]; | |
printf("<div class=\"timerBlock\" onclick=\"this.className += ' show';\">\r\n"); | |
printf("\t<p>Total Time: %0.3f seconds; Points: %d</p>", $lastTime - $firstTime, count($points)); | |
printf("\t<p>Points:</p>\r\n<ol>\r\n"); | |
$previousPoint = null; | |
foreach ($points as $p){ | |
printf("<li>%s: ", $p[0]); | |
printf("<span title=\"Since Start\">%0.3fs</span> ", | |
$p[1] - $firstTime | |
); | |
printf("<span title=\"Since previous\">(%0.3fs)</span>", | |
($previousPoint) ? $p[1] - $previousPoint[1] : '-' | |
); | |
$previousPoint = $p; | |
printf("</li>\r\n"); | |
} | |
printf("</ol></div>"); | |
}); | |
} | |
$points[] = [$label, microtime(1)]; | |
} | |
$iterations = 1000000; | |
$benchResult = []; | |
$n = 562; | |
markTime('Start'); | |
for ($i = 0; $i < $iterations; $i++){ | |
$result = floor($n / 10) * 10; | |
} | |
markTime('Method A'); | |
for ($i = 0; $i < $iterations; $i++){ | |
$result = round($n - 5, -1); | |
} | |
markTime('Method B'); | |
for ($i = 0; $i < $iterations; $i++){ | |
$result = $n - ($n % 10); | |
} | |
markTime('Method C'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment