0

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?

3
Contributors
8
Replies
9
Views
6 Years
Discussion Span
Last Post by diafol
0

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

0

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?

0

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

0

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.

...
2

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.

Votes + Comments
solution
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.