Skip to content

Instantly share code, notes, and snippets.

@Nex-Otaku
Created March 6, 2020 09:55
Show Gist options
  • Save Nex-Otaku/b7d013cdf6506b65cef9de4af2d61fe8 to your computer and use it in GitHub Desktop.
Save Nex-Otaku/b7d013cdf6506b65cef9de4af2d61fe8 to your computer and use it in GitHub Desktop.
<?php
namespace app\components\development\profiler;
class Profiler
{
/**
* @var float[]
*/
private $timers = [];
/**
* @param string $category
*/
public function startTimer(string $category = 'default'): void
{
$this->timers[$category] = $this->getMicrotime();
}
/**
* @param string $category
*/
public function finishTimer(string $category = 'default'): void
{
$getTimestampsTime = $this->getMicrotime() - $this->timers[$category];
$this->reportTime($category, $getTimestampsTime);
}
/**
* @param string $category
* @return float
*/
public function getTimer(string $category = 'default'): float
{
return $this->getMicrotime() - $this->timers[$category];
}
/**
* @return float
*/
private function getMicrotime(): float
{
return microtime(true);
}
/**
* @param string $category
* @param float $time
*/
private function reportTime(string $category, float $time): void
{
$message = sprintf('%s: %.2f сек', $category, $time);
echo "{$message}\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment