php中使用mysqli和pdo扩展,测试mysql数据库的执行效率。
<?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)."倍"; }
测试结果:其实经过多次测试,pdo和mysqli的执行效率差不多。