酷!學園
技術討論區 => Linux 討論版 => 主題作者是: Aeolus 於 2010-08-18 08:55
-
在網路上找到兩個小程式,參考 http://blog.linym.net/archives/103
count.php(修改後)
<?php
require_once( './Timer.php' );
$timer = new Timer();
$timer->start(); // 設定開始標籤
for ($i = 0; $i < 10000000; $i++);
$timer->stop(); // 設定結束標籤
$timer->display();
?>
Timer.php (php5)
<?php
class Timer {
private $markers;
public function __construct() {
$this->markers = array();
}
public function start() {
$this->setMarker('Start');
}
public function stop() {
$this->setMarker('Stop');
}
public function setMarker($name) {
$this->markers[$name] = $this->_getMicrotime();
}
public function _getMicrotime() {
$microtime = explode(' ', microtime());
return $microtime[1].substr($microtime[0], 1);
}
public function timeElapsed($start = 'Start', $end = 'Stop') {
if ($end == 'Stop' && !isset($this->markers['Stop'])) {
$this->markers['Stop'] = $this->_getMicrotime();
}
if (extension_loaded('bcmath')) {
return bcsub($this->markers[$end], $this->markers[$start], 6);
} else {
return $this->markers[$end] - $this->markers[$start];
}
}
public function display() {
print $this->timeElapsed();
}
}
?>
輸出為
1.346862
不知是算快?還是慢?
Thanks.
-
要測執行時間, 不要光是跑這種無意義的迴圈, 應該去測有『內容』的 php 碼
-
開xdebug做profiling可能比較有用吧?
...我想你可能只是想在網頁最下方顯示「本頁花了xxxx秒」這樣的訊息...那就在程式開始與結束包這些東西就可以了。不過要搭配template的話,使用$timer->timeElapsed()可能會比較有彈性。
-
要測執行時間, 不要光是跑這種無意義的迴圈, 應該去測有『內容』的 php 碼
謝謝,
簡單的測法,似乎無法檢測php的執行效率.
開xdebug做profiling可能比較有用吧?
...我想你可能只是想在網頁最下方顯示「本頁花了xxxx秒」這樣的訊息...那就在程式開始與結束包這些東西就可以了。不過要搭配template的話,使用$timer->timeElapsed()可能會比較有彈性。
小弟使用的IPB論壇程式,在首頁底下會有執行時間的訊息
因為自己的跟其他使用者,平均都差近4倍的時間
其他論壇
[attachment=1]
自己的主論壇,MySQL資料多
[attachment=2]
自己的測試論壇,MySQL資料非常少
[attachment=3]
當然論壇程式是除了論壇程式本身設定外,php , MySQL,這兩者也應該都會有影響
php加裝測試mem_cache模組,或 eAccesalator 或 APC 都無效.
MySQL也有啟用query cache
但似乎都沒改善,所以再想是否有什麼可測一下php ,MySQL的執行效率?
-
嗯,如果懷疑是mysql,那參考宗董這篇http://plog.longwin.com.tw/post/1/234 (http://plog.longwin.com.tw/post/1/234)來找找看問題。mysql cache沒記錯的話是用query string做hash key,容量有限,查詢結果超過cacahe就沒用,而且有新的query就可能被清掉...看你的使用狀況啦,很可能開了也沒有很大的效果。
-
嗯,如果懷疑是mysql,那參考宗董這篇http://plog.longwin.com.tw/post/1/234 (http://plog.longwin.com.tw/post/1/234)來找找看問題。mysql cache沒記錯的話是用query string做hash key,容量有限,查詢結果超過cacahe就沒用,而且有新的query就可能被清掉...看你的使用狀況啦,很可能開了也沒有很大的效果。
Thanks.
參閱設定後
有這樣訊息,(其中OOOOOO是管理者名稱,隱藏)
/usr/sbin/mysqld, Version: 5.0.51a-24+lenny4-log ((Debian)). started with:
Tcp port: 3306 Unix socket: /var/run/mysqld/mysqld.sock
Time Id Command Argument
# Time: 100818 14:02:45
# User@Host: debian-sys-maint[debian-sys-maint] @ localhost []
# Query_time: 10 Lock_time: 0 Rows_sent: 0 Rows_examined: 88482
SELECT COUNT(*) INTO @discard FROM `information_schema`.`COLUMNS`;
# Time: 100818 14:16:22
# User@Host: OOOOOO[OOOOOO] @ localhost []
# Query_time: 9 Lock_time: 0 Rows_sent: 1 Rows_examined: 88482
use information_schema;
SELECT COUNT(*) FROM `information_schema`.`COLUMNS`;
# Time: 100818 14:17:00
# User@Host: OOOOOO[OOOOOO] @ localhost []
# Query_time: 9 Lock_time: 0 Rows_sent: 30 Rows_examined: 30
SELECT * FROM `COLUMNS`
LIMIT 0, 30;
# Time: 100818 14:17:11
# User@Host: OOOOOO[OOOOOO] @ localhost []
# Query_time: 11 Lock_time: 0 Rows_sent: 1 Rows_examined: 88482
SELECT COUNT(*) FROM `information_schema`.`COLUMNS`;
# Time: 100818 14:17:20
# User@Host: OOOOOO[OOOOOO] @ localhost []
# Query_time: 9 Lock_time: 0 Rows_sent: 1 Rows_examined: 88482
SELECT COUNT(*) FROM `information_schema`.`COLUMNS`;
-
疑?網站程式為什麼要做這些查詢?這樣的查詢很沒意義...我想不出計算出所有的欄位數量有啥用處...
-
[attachment=1]
這個功能在論壇後台設定中可以關掉或是選擇其它顯示方式
主要是第一時間可清楚知道server負荷,產生首頁所花的時間,及查詢總量.
-
information_schema.COLUMNS裡面是資料庫中所有資料表的欄位的資訊,所以我會說做這樣的查詢沒有實際上的意義。你知道我的意思嗎?每個column各自有所數的資料表,個別查詢還OK,但是做count(*)真的不知道能做什麼...