In the top of the first page that is requested, I make a global variable and store the value of microtime() in it.

The footer of the page is require()'d at the bottom, and in the separate footer page is a code that subtracts the current microtime() from said global variable, giving (what I thought to be) an accurate time elapse of page creation.

Usually the time is anywhere between 0.0-0.3 with some extra digits not shown. Sometimes though it will appear as -0.7 or -0.8, randomly.

Does this have anything to do with the page loading the footer first because of the require(), THEN subtracting the actual microtime()? Or is this just a bug?

Loading time is not fixed every time.
It may differ. check this for any site.

Thanks for replying but that doesn't have much to do with what I'm asking.

Yes i do understand your point.
You have included file using require and your loading time is showing in minus for some reason.I think require should not create any issue. Posted link was just for sharing.
Can you post your php code for review?

Member Avatar for diafol

> Does this have anything to do with the page loading the footer first because of the require(), THEN subtracting the actual microtime()? Or is this just a bug?

Doubtful. Your php is probably procedural, so unless you've got call to functions or are using OOP PHP, commands/statements should run in sequence.

SHow your code.

You could add microtimes to an array and print those at the end to see what's going on?

Here's a condensed version of the code as-is:

(inside index.php)

<?php
  global $timeToWrite;
  $timeToWrite = microtime();

  ...

  ...

  ...

  require('pages/footer.php');

  ...

?>

(inside footer.php)

...

...

<p>Page written in <?php echo microtime() - $timeToWrite; ?> seconds.

...
Member Avatar for diafol

I don't understand why you're using global keyword. Probably no need.
If you're testing a lot of different elements:


ANyway, you need to use the true flag to change microtime into a float:

$t = microtime(true);
...
$s = microtime(true);
echo "Time: " . (($s - $t) * 1000) . "ms";

Mind you, I've read that gettimeofday(true) may be better.

commented: solution +1

thanks ardav, for whatever reason, passing microtime() with 'true' fixed the random negative value.

also, I guess I didn't need global, I think there was a reason for it but then I changed something and forgot about it.

Member Avatar for diafol

OK, we solved? If so, hit the link, mark as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.