Archive for February, 2010

Simple timer class for benchmarking in PHP

Wednesday, February 3rd, 2010

Here’s a simple timer class that I wrote to help with benchmarking tests while running PHP code, in order to see how quick code is running, although it can be used for any timer functionality really. I have clearly documented the code below, along with an example of how to use the timer

Timer Class Download

PHP Code
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*******************************************************************************
*                                     Timer class                              *
*                             Created: 27th January 2010                       *
*                             ©Copyright Jay Gilford 2010                      *
*                              http://www.jaygilford.com                       *
*                            email: jay [at] jaygilford.com                    *
*******************************************************************************/
 
 
class timer {
    private $_start = 0;
    private $_elapsed = 0;
    private $_running = false;
 
    /**
     * timer::construct()
     * set the timer running of $start is true
     * 
     * @param bool $start
     * @return true
     */
    public function __construct($start = false) {
        // If true is passed, start the timer immediately
        if($start === true)
            $this->start();
 
        return true;
    }
 
    /**
     * timer::start()
     * start the timer
     * 
     * @return true
     */
    public function start() {
        // Reset timer
        $this->reset(false);
        // Set status to running
        $this->_running = true;
        // Set timer running
        $this->_start = $this->_time();
 
        return true;
    }
 
    /**
     * timer::stop()
     * stops the timer and returns the total time elapsed
     * 
     * @return mixed
     */
    public function stop() {
        // If the timer isn't running return false
        if($this->_running === false)
            return false;
 
        // Set the final elapsed time and stop the timer
        $this->_elapsed = $this->_get_elapsed();
        $this->_running = false;
        $this->_start = 0;
 
        // Return the timers total elapsed time
        return $this->_elapsed;
    }
 
    /**
     * timer::pause()
     * pauses/unpauses the timer
     * 
     * @return NULL
     */
    public function pause() {
        // If the timer isn't running return false
        if($this->_running === false)
            return false;
 
        // If the timer is paused set the timer from the current time
        if($this->_start == 0) {
            $this->_start = $this->_time();
 
        // Otherwise add the currently elapsed time to the total
        // elapsed time and pause the timer
        }else{
            $this->_elapsed = $this->_get_elapsed();
            $this->_start = 0;
        }
    }
 
    /**
     * timer::elapsed()
     * returns the total time elapsed since the timer started
     * 
     * @return float
     */
    public function elapsed() {
        return $this->_get_elapsed();
    }
 
    /**
     * timer::reset()
     * resets the timers variables back to default
     * 
     * @param bool $start
     * @return
     */
    public function reset($start = false) {
        // Reset all variables
        $this->_start = 0;
        $this->_elapsed = 0;
        $this->_running = false;
 
        // If $start is true restart the timer immediately
        if($start === true)
            $this->start();
 
        return true;        
    }
 
    /**
     * timer::_time()
     * returns the current unix time (with milliseconds)
     * 
     * @return float
     */
    private function _time() {
    	$time = explode(' ', microtime());
    	$time =  (float)$time[1] + $time[0];
        return $time;
    }
 
    /**
     * timer::_get_elapsed()
     * Returns the current elapsed time
     * 
     * @return float
     */
    private function _get_elapsed() {
        if($this->_running === false || $this->_start == 0)
            return $this->_elapsed;
 
        return $this->_elapsed + ($this->_time() - $this->_start);
    }
}

Here is a simple example of how to use the class for working out SQL execution times

PHP Code
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
include 'timer.class.php';
$db_timer = new timer();
 
// Connect to database here and create the query to be run
mysql_connect('localhost', 'root', '');
mysql_select_db('database');
 
 
$query = "SELECT * FROM table";
 
// Start timer before executing query
$db_timer->start();
$result = mysql_query($query);
 
// Pause timer
$db_timer->pause();
 
// Echo out the selected data
while($row = mysql_fetch_assoc($result)) {
    echo print_r($row, true);
}
 
// Create another query
$query = "SELECT * FROM table2";
 
// Unpause the timer
$db_timer->pause();
 
// Execute the query
$result = mysql_query($query);
 
// Stop the timer and get the total execution time
$total_time = $db_timer->stop();
 
// Display details
echo 'SQL queries took '.$total_time.' seconds to execute in total';

If you have any questions or comments including possible improvements feel free to comment or send me a message via the contact page