Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've created a basic application in PHP and would like to print the time it takes to load the page at the end of the page. I first define the start with a microtime and then compare it to the current microtime at the end, the problem is it outputs something like "0.000102" and I'm looking for it in milliseconds, I'm guessing that would be 102ms?

Define the start,

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

Then print the end time.

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

But it still outputs that horrible long string.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
310 views
Welcome To Ask or Share your Answers For Others

1 Answer

Milliseconds is n*1000:

<?php
$start = microtime(true);

usleep(1000000);

$end = microtime(true) - $start;

printf("Page was rendered in %f seconds", $end);
printf("Page was rendered in %f milliseconds", $end*1000);
printf("Page was rendered in %f microseconds", ($end*1000)*1000);

https://3v4l.org/kHmA3

Result:

Page was rendered in 1.000115 seconds
Page was rendered in 1000.115156 milliseconds
Page was rendered in 1000115.156174 microseconds

Edit: If you want values outputted like 0.10 etc you will need to change %f to %s and use round().

<?php
$start = microtime(true);

usleep(1000000);

$end = microtime(true) - $start;

printf("Page was rendered in %s seconds", round($end, 2));
printf("Page was rendered in %s milliseconds", round($end*1000, 2));
printf("Page was rendered in %s microseconds", round(($end*1000)*1000, 2));

https://3v4l.org/OfgJX

Result:

Page was rendered in 1 seconds
Page was rendered in 1000.12 milliseconds
Page was rendered in 1000121.12 microseconds

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...