Hi,
i would like to know how people are able to display how long it takes for the page to be generated (for example Invision Board)
It says at the bottom X Quires in XXXX Secs. How would i get that to be displayed?
How to find the time an action took
Moderator: General Moderators
- Buddha443556
- Forum Regular
- Posts: 873
- Joined: Fri Mar 19, 2004 1:51 pm
Code: Select all
// Beginning of the script
$start_time = microtime();Code: Select all
// initialize qCount
$qCount = 0;
// replace all mysql_query() with my_query()
function my_query($sql) {
$result = mysql_query($sql);
$qCount++;
}Code: Select all
// End of the Script
$start_time = explode(" ", $start_time);
$end_time = explode(" ", microtime());
$exec_time = ((float)$end_time[1] + (float)$end_time[0]) - ((float)$start_time[1] + (float)$start_time[0]);Code: Select all
// output the info
echo "$qCount Quires in $exec_time Secs";- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
// initialize qCount
$qCount = 0;
// replace all mysql_query() with my_query()
function my_query($sql) {
$result = mysql_query($sql);
$qCount++;
}1) $qCount = 0 is not in the same scope as that in the $qCount++
2) the $qCount in the function will not persist through multiple function calls
I generally use a db abstraction layer, combine with a singleton pattern (or look into static variables) in combination with my mysql calls so I can count the queries (sort of like what you did, but properly)
another option would be to use sessions, so something like
Code: Select all
session_start();
//format microtime float
function microtime_float() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function my_query($sql) {
$_SESSION['queryCount']++;
return mysql_query($sql) or die(mysql_error());
}
//reset counter
$_SESSION['queryCount'] = 0;
//initialize benchmark time
$time_start = microtime_float();
/* BENCHMARK STARTS HERE */
$result1 = my_query('SELECT * FROM `users`');
$result2 = my_query('SELECT * FROM `users`');
$result3 = my_query('SELECT * FROM `users`');
/* BENCHMARK ENDS HERE */
//initialize benchmark end
$time_end = microtime_float();
$time = $time_end - $time_start;
echo 'Queries: '. $_SESSION['queryCount'].'. Execution: '. $time .' seconds';- Buddha443556
- Forum Regular
- Posts: 873
- Joined: Fri Mar 19, 2004 1:51 pm
Told you it was pass my bedtime.Jcart wrote:This simply will not work for the following reasons:Code: Select all
// initialize qCount $qCount = 0; // replace all mysql_query() with my_query() function my_query($sql) { $result = mysql_query($sql); $qCount++; }
1) $qCount = 0 is not in the same scope as that in the $qCount++
2) the $qCount in the function will not persist through multiple function calls
I generally use a db abstraction layer, combine with a singleton pattern (or look into static variables) in combination with my mysql calls so I can count the queries (sort of like what you did, but properly)
another option would be to use sessions, so something like