0

I'm trying to check how long it took to load my page in PHP, this means created a start and end time and checking the differences at the end of the script.

I do this with microtime and then print out how long it actually took, I want to print it out using miliseconds, so lets say it took 269 milliseconds to load, it would display the following.

Page was rendered in 269 milliseconds.

I also have an extension on my browser that displays how long the page took, is this load the extension said 82ms, now I know they can vary but I'm just putting that out there.

My page displays 0.28086708618164 but surely it can't be that low? I'm not loading any assets, images, css files or anything so the gap between the extension and PHP really shouldn't be that much, I'm hoping you guys agree...

Here is my code:

<?php declare(strict_types = 1);

define("START", microtime(true));

// execute some code...

printf("Page was rendered in %s milliseconds", (microtime(true) - START) * 1000);
guziba
  • 21
  • 3
  • I think you're measuring different things. The browser doesn't start rendering until the PHP script finishes and sends its buffer to the client. – Barmar Feb 10 '18 at 21:02
  • You can use the Network tab in the browser to see the time between sending the `GET` request and receiving the response. – Barmar Feb 10 '18 at 21:03
  • @Barmar either way, the PHP code still isn't working correctly. – guziba Feb 10 '18 at 21:06
  • What makes you say that? – Barmar Feb 10 '18 at 21:08
  • @Barmar It's taking 0.04 milliseconds, which is 4 microseconds, I'm pretty sure PHP can't run that fast. It also says the GET request in Chrome's network tab takes 6ms. – guziba Feb 10 '18 at 21:20
  • Where do you see that? The question says 269 milliseconds. – Barmar Feb 10 '18 at 21:21
  • Read my question again, I was giving an example of IF the page was loaded in 269, 269 would be what I want to output. – guziba Feb 10 '18 at 21:24
  • Pleae clarify the question so it shows the actual output and explains why you think it's wrong. – Barmar Feb 10 '18 at 21:26
  • I have, if you read the question clearly you will too. – guziba Feb 10 '18 at 21:30
  • 1
    @guziba why are you multi-accounting? I answered this question an hour ago lol https://stackoverflow.com/questions/48724704/time-it-took-to-load-in-php/48724810#48724810 – Lawrence Cherone Feb 10 '18 at 21:30
  • I don't see "4 microseconds" anywhere in the question. Is `0.28086708618164` what the PHP is saying or what the browser extension is saying? – Barmar Feb 10 '18 at 21:31
  • 0.04 milliseconds = 40 microseconds, not 4 microseconds. – Barmar Feb 10 '18 at 21:32

2 Answers2

0

It could be because of many different reasons. Are you sure your browser extension is calculating as you expect ?

I used this in a code lately :

$SearchMSC = microtime(true);
... code ...
$SearchMSC = microtime(true)-$SearchMSC;
$SearchMSC = round(($SearchMSC * 100),2);

Can you try a different browser to compare to see how much off you are ?

Bryan Meyer
  • 103
  • 13
  • I've just calculated using different browsers (with your code), Chrome: (0.03 in content, 0.01 in extension), Firefox: (0.04 in content, 67ms in extension). It still seems unbelievable that PHP is finishing in 0.04 milliseconds, which is 4 microseconds. Update; in Chrome's network tab, it shows the document takes 6ms to load. – guziba Feb 10 '18 at 21:17
  • Your code would produce 100.00ms for 1 second, hope your not relying on this. https://3v4l.org/JbMDS – Lawrence Cherone Feb 10 '18 at 21:39
0

Lets say it took 269 milliseconds to load, it would display the following.

Page was rendered in 269 milliseconds.

Then you must round down the float:

<?php 
define("START", microtime(true));

//sleep for 269 milliseconds
usleep(269000);

printf("Page was rendered in %s milliseconds", round((microtime(true) - START) * 1000));

https://3v4l.org/r69IY

Result:

Page was rendered in 269 milliseconds

If you are getting 0.26986708618164, then your not multiplying it by 1000 and rounding it.

<?php 
define("START", microtime(true));

//sleep for 269 milliseconds
usleep(269000);

printf("Page was rendered in %s milliseconds", microtime(true) - START);

https://3v4l.org/Uc80g

Result:

Page was rendered in 0.26908707618713 milliseconds

Which is wrong because its not in milliseconds, its showing 0.26 of 1 second not 0.26 of 1 millisecond.

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106