php使用mysqli和pdo扩展,测试对比mysql数据库的执行效率完整示例
本文实例讲述了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的执行效率差不多。
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP基于pdo操作数据库技巧总结》、《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
相关推荐
yifangs 2020-10-13
zyyjay 2020-11-09
xuebingnan 2020-11-05
samtrue 2020-11-22
stefan0 2020-11-22
songshijiazuaa 2020-09-24
hebiwtc 2020-09-18
天步 2020-09-17
83911535 2020-11-13
whatsyourname 2020-11-13
zhouyuqi 2020-11-10
Noneyes 2020-11-10
mathchao 2020-10-28
王志龙 2020-10-28
wwwsurfphpseocom 2020-10-28
diskingchuan 2020-10-23
savorTheFlavor 2020-10-23