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?

Dani AI

Generated

Short answer: the negative times were caused by using the default string form of microtime() and not by require() or include order. When microtime() is called without the boolean flag it returns a string with the fractional part first and the whole seconds second. In numeric contexts PHP converts the string starting from the left — so only the fractional part is used. If the second boundary ticks over between your two samples the fractions alone can give a negative result (for example 0.1 - 0.9 = -0.8) even though the real elapsed time is positive. was correct to suggest returning a float; that resolves the parsing issue. See the PHP manual on [microtime()] (https://www.php.net/manual/en/function.microtime.php).

Including files does not change execution order or scope in this case: an included/required file runs right where the statement appears and inherits the including file's variables, so the global keyword is unnecessary. See [include()] (https://www.php.net/manual/en/function.include.php).

For modern, robust timing (avoid float rounding, system-clock jumps, or very high resolution) use a monotonic timer such as hrtime() (PHP 7.3+). Example:

$start_ns = hrtime(true);
# ... work ...
$elapsed_ms = (hrtime(true) - $start_ns) / 1e6;
echo $elapsed_ms . " ms";

See [hrtime()] (https://www.php.net/manual/en/function.hrtime.php). For typical page-response timing microtime(true) is fine; use hrtime for benchmarking or when you need nanosecond precision or monotonic behavior.

Recommended Answers

All 8 Replies

Loading time is not fixed every time.
It may differ. check 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 Member #120589

> 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 Member #120589

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 Member #120589

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.