Skip to content

Instantly share code, notes, and snippets.

@kicken
Created March 1, 2018 02:43
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 kicken/51c69b70fc8bd73e6a0fcc659641cd3b to your computer and use it in GitHub Desktop.
Save kicken/51c69b70fc8bd73e6a0fcc659641cd3b to your computer and use it in GitHub Desktop.
Time logging
<?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