Skip to content

Instantly share code, notes, and snippets.

@dtomasi
Last active August 31, 2018 20:01
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 dtomasi/1b245bf65663722dece8 to your computer and use it in GitHub Desktop.
Save dtomasi/1b245bf65663722dece8 to your computer and use it in GitHub Desktop.
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