LooieENG Posted September 1, 2008 Share Posted September 1, 2008 Here's the code I have conf.php <?php $stime = microtime(); $queries = 0; ?> Page with microtime <?php require('conf.php'); ?> ... Page Generated in <?php echo microtime() - $stime ?> seconds with <?php echo $queries . ' MySQL ' . ($queries == 1 ? 'Query' : 'Queries') ?> And here's what it returns Page Generated in 1.5E-05 seconds with 0 MySQL Queries Quote Link to comment Share on other sites More sharing options...
blinky001 Posted September 1, 2008 Share Posted September 1, 2008 Try this: <?php class Timing { private $store; public function __construct($name = 'default') { $this->store[$name]['start'] = explode(' ', microtime()); } public function stop($name = 'default') { $this->store[$name]['stop'] = explode(' ', microtime()); } public function getTime($name = 'default') { if(!isset($this->store[$name]['stop'])) { $this->stop($name); } $seconds = $this->store[$name]['stop'][1] - $this->store[$name]['start'][1]; $micros = $this->store[$name]['stop'][0] - $this->store[$name]['start'][0]; return $seconds + $micros; } } ?> Usage: <?php $timer = new Timer(); ## Whatever..... echo $timer->getTime(); ?> You need to include the class, and then initialise the timer in your bootstrap/setup file. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted September 1, 2008 Share Posted September 1, 2008 For calculating time differences, microtime() needs to be called using: microtime(true) Without the parameter, it returns a string like this: 0.17188000 1220288104 and the subtraction is not doing what you think it is. Quote Link to comment Share on other sites More sharing options...
LooieENG Posted September 1, 2008 Author Share Posted September 1, 2008 blinky001, thanks for the reply, but I don't really understand classes, and that looks like a lot of code for something simple. PFMaBiSmAd, could it be Webserver/PHP version? The same code works on my shared host using Apache and PHP 5.2.5. My server is using lighttpd and php5 fastcgi (5.2.0- Edit: echo microtime(); returns 0.3624646 2462627 (example) on both servers, so it must be a PHP config problem, right? Quote Link to comment Share on other sites More sharing options...
blinky001 Posted September 1, 2008 Share Posted September 1, 2008 Classes are pretty simple... If you have a directory for includes just make a new file and put in that class info. Then include and use it. E.g. <?php include('../includes/core.php'); include('../includes/timing.php'); $timer = new Timer(); # All the rest of your page here echo $timer->getTime(); ?> The reason it is a bit more complicated is that it allows you to make new names (or something) - which is totally not required (not my class). Quote Link to comment Share on other sites More sharing options...
peranha Posted September 1, 2008 Share Posted September 1, 2008 if you dont want to use classes you can use this function. // Function to calculate time taken to load a page. function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); // At beginning of page add '$start = microtime_float();' without the quotes. // At the end of the page add 'substr(microtime_float() - $start, 0, 5)' without the quotes. Change the 5 to whatever number of digits you want to display. 5 will output 0.000 Quote Link to comment Share on other sites More sharing options...
LooieENG Posted September 1, 2008 Author Share Posted September 1, 2008 This is strange, I added $con = mysql_connect('', '', ''); mysql_select_db(''); to conf.php and now it works Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.