用PHP框架与原始代码读取Mysql单条数据性能比较

重复读取以下代码5万次

select * from pin_article limit 1

 

PHP框架(用ThinkPHP为代表 ) 费时:22.237736940384秒,测试代码

<?php
// 本类由系统自动生成,仅供测试用途
class IndexAction extends Action {
public function index(){
$this->show('thinkphp');
}
public function test(){

$pagestartime=microtime();

$articleM_mod = M('article');
for($i=0;$i<50000;$i++){
$var = $articleM_mod->where("id=1")->select();
}
//var_dump($var);
//echo 1;
$pageendtime = microtime();
$starttime = explode(" ",$pagestartime);
$endtime = explode(" ",$pageendtime);
$totaltime = $endtime[0]-$starttime[0]+$endtime[1]-$starttime[1];
$timecost = sprintf("%s",$totaltime);
echo $timecost;
}
}

?>

ECSHOP的mysql类 费时:14.953320980072秒,测试代码

<?php
$pagestartime=microtime();
include_once 'db.php';
$sql = "select * from pin_article limit 1";

for($i=0;$i<50000;$i++){
$var = $db->getAll($sql);
}
$pageendtime = microtime();
$starttime = explode(" ",$pagestartime);
$endtime = explode(" ",$pageendtime);
$totaltime = $endtime[0]-$starttime[0]+$endtime[1]-$starttime[1];
$timecost = sprintf("%s",$totaltime);
echo $timecost;
?>

 

原始代码 费时:10.879239082336秒,测试代码

<?php
$pagestartime=microtime();
$mysql_server_name='localhost';
$mysql_username='root';
$mysql_password='';
$mysql_database='pinphp3';
$sql = "select * from pin_article limit 1";
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password);
mysql_select_db($mysql_database,$conn);
for($i=0;$i<50000;$i++){
$result=mysql_query($sql);
$result = mysql_fetch_row($result);

}
mysql_close($conn);
//var_dump($result);
$pageendtime = microtime();
$starttime = explode(" ",$pagestartime);
$endtime = explode(" ",$pageendtime);
$totaltime = $endtime[0]-$starttime[0]+$endtime[1]-$starttime[1];
$timecost = sprintf("%s",$totaltime);
echo $timecost;

?>

 

 

 

 

Tags:

发表评论