美文网首页
[PDO]①⑦--效率分析

[PDO]①⑦--效率分析

作者: 子木同 | 来源:发表于2017-09-05 14:58 被阅读3次
Paste_Image.png

连接数据库效率

<?php
//1.通过PDO连接数据库
$pStartTime = microtime(true);
for ($i = 1; $i <= 100; $i++) {
    $pdo = new PDO('mysql:host=localhost;dbname=test', 'root', '');
}
$pEndTime = microtime(true);
$res1 = $pEndTime - $pStartTime;

//2.通过MySQL连接数据库
$mStartTime = microtime(true);
for ($i = 1; $i <= 100; $i++) {
    mysql_connect('localhost', 'root', '');
    mysql_selectdb('test');
}
$mEndTime = microtime(true);
$res2 = $mEndTime - $mStartTime;
echo $res1, '<br/>', $res2;
echo '<hr/>';

if ($res1 > $res2) {
    echo 'PDO连接数据库MYSQL的 ' . round($res1 / $res2) . '倍';
} else {
    echo 'MYSQL连接数据库PDO的 ' . round($res2 / $res1) . '倍';
}

?>
Paste_Image.png

插入记录效率

mysql> CREATE TABLE test2(
    -> id INT
    -> );
Query OK, 0 rows affected (0.05 sec)
<?php
header('content-type:text/html;charset=utf-8');
//1.通过PDO连接数据库
$pStartTime=microtime(true);
$pdo=new PDO('mysql:host=localhost;dbname=test','root','');
$sql='INSERT test2 VALUES(:id)';
$stmt=$pdo->prepare($sql);
for($i=1;$i<=500;$i++){
    $id=1;
    $stmt->bindParam(':id', $id,PDO::PARAM_INT);
    $stmt->execute();
}
$pEndTime=microtime(true);
$res1=$pEndTime-$pStartTime;
unset($pdo);//$pdo=null;
//2.通过MySQL连接数据库
$mStartTime=microtime(true);
mysql_connect('localhost','root','');
mysql_select_db('test');
for($i=1;$i<=500;$i++){
    $sql='INSERT test2 VALUES(2)';
    mysql_query($sql);
}
mysql_close();
$mEndTime=microtime(true);
$res2=$mEndTime-$mStartTime;
echo $res1,'<br/>',$res2;
echo '<hr/>';
if($res1>$res2){
    echo 'PDO插入500条记录的是MySQL'.round($res1/$res2).'倍';
}else{
    echo 'MySQL插入500条记录的是PDO的'.round($res2/$res1).'倍';
}

Paste_Image.png

相关文章

网友评论

      本文标题:[PDO]①⑦--效率分析

      本文链接:https://www.haomeiwen.com/subject/vjmujxtx.html