Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

Time it took to load in PHP?

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

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

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...