pdo 性能测试

1、插入500条记录到mysql test2表中。

建表

CREATE TABLE IF NOT EXISTS test2(id INT);

php插入代码

<?php
//1.利用pdo连接数据库,测试效率
    header('contentType:text/html;charset:utf-8');
    $pStart=microtime(true);
    $pdo=new PDO('mysql:host=localhost;dbname=imooc','root','playboy');
    $sql='insert into test2 values(:id)';
    $stmt=$pdo->prepare($sql);
    for($i=0;$i<500;$i++){
        $id=1;
        $stmt->bindParam(':id',$id,PDO::PARAM_INT);
        $stmt->execute();
    }
    $pEnd=microtime(true);
    $LastingTP=$pEnd-$pStart;
    unset($pdo);  //销毁对象,释放资源


//2、利用mysql连接数据库
    $mysqlStart=microtime(true);
    mysql_connect('localhost','root','playboy');
    mysql_select_db('imooc');
    for($i=0;$i<500;$i++){
        $sql='insert intpo test2  values(2)';
        mysql_query($sql);
    
    }
    mysql_close();
    $mysqlEnd=microtime(true);
    $LastingTM=$mysqlEnd-$mysqlStart;
    echo $LastingTP.'<br/>';
    echo $LastingTM.'<br/>';
    if($LastingTP>$LastingTM){
        echo 'pdo插入500条数据到数据库时间是mysql的'.$LastingTP/$LastingTM.'倍';
    }else{
    echo 'mysql插入500条数据到数据库时间的'.$LastingTM/$LastingTP.'倍';    
    }
?>

2、php利用pdo连接数据库和mysql_connect效率比较

<?php
//1.利用pdo连接数据库,测试效率
    header('contentType:text/html;charset:utf-8');
    $pStart=microtime(true);
    for($i=0;$i<100;$i++){
    $pdo=new PDO('mysql:host=localhost;dbname=imooc','root','playboy');
    
    }
    $pEnd=microtime(true);
    $LastingTP=$pEnd-$pStart;



//2、利用mysql连接数据库
    $mysqlStart=microtime(true);
    for($i=0;$i<100;$i++){
    mysql_connect('localhost','root','playboy');
    mysql_select_db('imooc');
    
    }
    $mysqlEnd=microtime(true);
    $LastingTM=$mysqlEnd-$mysqlStart;
    echo $LastingTP.'<br/>';
    echo $LastingTM.'<br/>';
    if($LastingTP>$LastingTM){
        echo 'pdo连接数据库时间是mysql连接数据库时间的'.$LastingTP/$LastingTM.'倍';
    }else{
    echo 'mysql连接数据库时间是pdo连接数据库时间的'.$LastingTM/$LastingTP.'倍';    
    }
?>


转载于:https://my.oschina.net/u/1778933/blog/470257

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值