Tính thời gian thực hiện trang hoặc câu lệnh trong PHP

361 views
Skip to first unread message

php5developer

unread,
Oct 26, 2004, 12:31:22 PM10/26/04
to phpvi...@googlegroups.com
chào mọi người,

tôi thấy nhiều diễn đàn có cái thông báo: trang này
được xử lý trong xxx giây. Có ai biết giải thuật để
làm việc đó như thế nào không?

Rascator

unread,
Oct 26, 2004, 2:15:04 PM10/26/04
to phpvi...@googlegroups.com

Chào bác

> tôi thấy nhiều diễn đàn có cái thông báo: trang này được xử
> lý trong xxx giây. Có ai biết giải thuật để làm việc đó như
> thế nào không?

Cái này em đã từng xem trong manual của PHP, một ví dụ về microtime()

usage: string microtime ( )
available since: PHP 3, PHP 4

Function này sẽ trả lại string dạng "msec sec".
- sec is the current time measured in the number of seconds since the Unix Epoch (0:00:00 January 1, 1970 GMT)
- msec is the microseconds part.

This function is only available on operating systems that support the gettimeofday() system call.
Both portions of the string are returned in units of seconds.

Đây là đọan code ví dụ em lấy trong PHP Manual, có thêm mấy cái echo để biết chính xác cái gì xảy ra

<?php
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}

$time_start = getmicrotime();
echo "Start at ".$time_start."<br>";

for ($i=0; $i < 10; $i++){
//do nothing, 10 times
echo ("usec sec: ".microtime()."<br>");
}

$time_end = getmicrotime();
echo "End at ".$time_end."<br>";
$time = $time_end - $time_start;

echo "Did nothing in $time seconds";
?>

pcdinh

unread,
Oct 27, 2004, 11:35:38 AM10/27/04
to phpvi...@googlegroups.com
Về mặt giải thuật thì có thể mô tả như sau:

- PHP phân tích các câu lệnh từ trên xuống dưới cho nên
việc đo thời gian cũng giống như đo đường, đo thời
gian chạy thi

- Trước câu lệnh thì bấm giờ, hết câu lệnh bấm giờ
tiếp. Thời gian thực thi là giờ sau trừ giờ trước..
Dùng hàm time() hay microtime() để bấm giờ

hoangek

unread,
Oct 27, 2004, 12:04:15 PM10/27/04
to phpvi...@googlegroups.com

Toi nghi la no lay o dau do, vi trong phpmyadmin co
cai do, neu ban sieng mo ra doc thu coi



__________________________________
Do you Yahoo!?
Read only the mail you want - Yahoo! Mail SpamGuard.
http://promotions.yahoo.com/new_mail
Reply all
Reply to author
Forward
0 new messages