timer.php
Gesponsorde koppelingen
PHP script bestanden
Code (php)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
* Timer
*
* The basic timer function
*/
class Timer
{
/**
* All markers with the times
*
* @access protected
* @var Array
* @static
*/
protected static $markers = Array();
/**
* A shortcut for start marker
*
* @return void
* @see Timer::setMarker()
* @static
*/
public static function start()
{
self::setMarker('start');
}
/**
* A shortcut for end marker
*
* @return void
* @see Timer::setMarker()
* @static
*/
public static function end()
{
self::setMarker('end');
}
/**
* Sets a time marker in the script
*
* @param string $name The name of the marker
* @return void
* @static
*/
public static function setMarker( $name )
{
self::$markers[$name] = microtime(true);
}
/**
* A shortcut for getDiff( 'start', 'end' )
*
* @return int The difference between start and end
* @see Timer::getDiff()
* @static
*/
public static function getResult()
{
return self::getDiff('end', 'start');
}
/**
* Get the difference between 2 markers
*
* @param string $marker1 The name of the first marker
* @param string $marker2 The name of the second marker
* @return int The difference between the times
* @static
*/
public static function getDiff( $marker1, $marker2 )
{
if( !isset(self::$markers[$marker1]) )
throw new InvalidArgumentException('The Marker('.$marker1.' has not been set');
if( !isset(self::$markers[$marker2]) )
throw new InvalidArgumentException('The Marker('.$marker2.' has not been set');
$diff = (self::$markers[$marker1] - self::$markers[$marker2]);
if( $diff < 0 )
$diff *= -1;
return $diff;
}
}
/**
* Timer
*
* The basic timer function
*/
class Timer
{
/**
* All markers with the times
*
* @access protected
* @var Array
* @static
*/
protected static $markers = Array();
/**
* A shortcut for start marker
*
* @return void
* @see Timer::setMarker()
* @static
*/
public static function start()
{
self::setMarker('start');
}
/**
* A shortcut for end marker
*
* @return void
* @see Timer::setMarker()
* @static
*/
public static function end()
{
self::setMarker('end');
}
/**
* Sets a time marker in the script
*
* @param string $name The name of the marker
* @return void
* @static
*/
public static function setMarker( $name )
{
self::$markers[$name] = microtime(true);
}
/**
* A shortcut for getDiff( 'start', 'end' )
*
* @return int The difference between start and end
* @see Timer::getDiff()
* @static
*/
public static function getResult()
{
return self::getDiff('end', 'start');
}
/**
* Get the difference between 2 markers
*
* @param string $marker1 The name of the first marker
* @param string $marker2 The name of the second marker
* @return int The difference between the times
* @static
*/
public static function getDiff( $marker1, $marker2 )
{
if( !isset(self::$markers[$marker1]) )
throw new InvalidArgumentException('The Marker('.$marker1.' has not been set');
if( !isset(self::$markers[$marker2]) )
throw new InvalidArgumentException('The Marker('.$marker2.' has not been set');
$diff = (self::$markers[$marker1] - self::$markers[$marker2]);
if( $diff < 0 )
$diff *= -1;
return $diff;
}
}