Jump to content

[SOLVED] Microtime() not working :/


LooieENG

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/122251-solved-microtime-not-working/
Share on other sites

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.

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-8)

 

Edit: echo microtime(); returns 0.3624646 2462627 (example) on both servers, so it must be a PHP config problem, right?

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).

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.