Blame view

common/helpers/Timer.php 849 Bytes
2e86c939   xu   “首次提交”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?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'];
    }
}