?php
/**
* 测试pdo和mysqli的执行效率
*/
header("Content-type:text/html;charset=utf-8");
//通过pdo链接数据库
$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标题".$i;
$content = "pdo内容".$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链接数据库
$mysqli_startTime = microtime(true);
$mysqli = mysqli_connect("localhost","root","1234","test") or die("数据连接失败");
mysqli_query($mysqli,"set names utf8");
for($i=1;$i=100;$i++){
$title = "mysqli标题".$i;
$content = "mysqli内容".$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的执行时间是mysqli的".round($pdo_time/$mysqli_time)."倍";
}else{
echo "mysqli的执行时间是pdo的".round($mysqli_time/$pdo_time)."倍";
}
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP基于pdo操作数据库技巧总结》、《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》