Timer.php 849 Bytes
<?php

namespace common\helpers;

/**
 * Class Timer
 */
class Timer
{
    private static $timers;

    /**
     * @param $name
     */
    public static function start($name)
    {
        self::$timers[$name]['start'] = microtime(true);
    }

    /**
     * Reads the current timer value without stopping the timer.
     *
     * @param $name The name of the timer.
     * @return The current timer value in ms.
     */
    public static function read($name)
    {
        if (isset(self::$timers[$name]['start'])) {
            $stop = microtime(true);
            $diff = round(($stop - self::$timers[$name]['start']) * 1000, 2);

            if (isset(self::$timers[$name]['time'])) {
                $diff += self::$timers[$name]['time'];
            }
            return $diff;
        }
        return self::$timers[$name]['time'];
    }
}