?php
/**
* 測試pdo和mysqli的執(zhí)行效率
*/
header("Content-type:text/html;charset=utf-8");
//通過pdo鏈接數(shù)據(jù)庫
$pdo_startTime = microtime(true);
$pdo = new PDO("mysql:host=localhost;dbname=test","root","1234",array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES'utf8';"));
for($i=1;$i=100;$i++){
$title = "pdo標(biāo)題".$i;
$content = "pdo內(nèi)容".$i;
$addtime = time();
$user_id = $i;
$pdo_sql = "INSERT INTO `article`(`title`,`content`,`addtime`,`user_id`) VALUES(:title,:content,:addtime,:user_id)";
$sth = $pdo->prepare($pdo_sql);
$sth->bindParam(':title',$title);
$sth->bindParam(':content',$content);
$sth->bindParam(':addtime',$addtime);
$sth->bindParam(':user_id',$user_id);
$sth->execute();
}
$pdo_endTime = microtime(true);
$pdo_time = $pdo_endTime - $pdo_startTime;
echo $pdo_time;
echo "hr/>";
//通過mysql鏈接數(shù)據(jù)庫
$mysqli_startTime = microtime(true);
$mysqli = mysqli_connect("localhost","root","1234","test") or die("數(shù)據(jù)連接失敗");
mysqli_query($mysqli,"set names utf8");
for($i=1;$i=100;$i++){
$title = "mysqli標(biāo)題".$i;
$content = "mysqli內(nèi)容".$i;
$addtime = time();
$user_id = $i;
$sql = "INSERT INTO `article`(`title`,`content`,`addtime`,`user_id`) VALUES('".$title."','".$content."',".$addtime.",".$user_id.")";
mysqli_query($mysqli,$sql);
}
$mysqli_endTime = microtime(true);
$mysqli_time = $mysqli_endTime - $mysqli_startTime;
echo $mysqli_time;
echo "hr/>";
if($pdo_time > $mysqli_time){
echo "pdo的執(zhí)行時間是mysqli的".round($pdo_time/$mysqli_time)."倍";
}else{
echo "mysqli的執(zhí)行時間是pdo的".round($mysqli_time/$pdo_time)."倍";
}
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《php+mysqli數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》