How to find the time an action took

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

How to find the time an action took

Post by a94060 »

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?
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

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";
No guarantee any of that works, it's pass my bedtime. :oops:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

// initialize qCount
$qCount = 0;

// replace all mysql_query() with my_query()
function my_query($sql) {
  $result = mysql_query($sql);
  $qCount++;
}
This simply will not work for the following reasons:

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';
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

ok,well,i try those a little bit later beaucse it is way past my bed time here (11:19 EST here)

later all.
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Post by Buddha443556 »

Jcart wrote:

Code: Select all

// initialize qCount
$qCount = 0;

// replace all mysql_query() with my_query()
function my_query($sql) {
  $result = mysql_query($sql);
  $qCount++;
}
This simply will not work for the following reasons:

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
Told you it was pass my bedtime. :lol: But yeah I need a global in there.
Post Reply