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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.