Skip to content

Instantly share code, notes, and snippets.

@dtomasi dtomasi/Timer.php
Last active Aug 31, 2018

Embed
What would you like to do?
Timer-Class for Testing speed of PHP-Scripts or find speed brake
<?php
/**
* Timer vor Testing PHP-Scripts
* @copyright tomasiMEDIA 2013
* @author Dominik Tomasi
* @date 09.12.13
*
* usage:
* Timer::start();
*
* somewhere in your Code do -> Timer::newStop('nameOfEntry');
*
* at end of script do -> Timer::getResults();
* or as String -> Timer::getResultsAsString();
*
*/
class Timer
{
/**
* StartTime
* @var null
*/
private static $startTime = null;
/**
* Array of Timers
* @var array
*/
private static $arrTimers = array();
/**
* Get Time
* @return float
*/
private static function getmicrotime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
/**
* Start Timer
*/
public static function start()
{
self::$startTime = self::getmicrotime();
}
/**
* Stop a Time an give a Name
* @param $strName
* @throws \ErrorException
*/
public static function newStop($strName)
{
if (is_null(self::$startTime))
{
throw new \ErrorException('Timer: start method not called !');
}
self::$arrTimers[$strName] = round((self::getmicrotime() - self::$startTime), 4);
}
/**
* Get Timer Results as Array
* @return array
*/
public static function getResults()
{
return self::$arrTimers;
}
/**
* Return the Timer-Array as printable String
* @return string
*/
public static function getResultAsString()
{
ob_start();
var_dump(self::$arrTimers);
$result = ob_get_clean();
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.