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.'倍';
}
?>