how fast is PHP PDO versus PEAR DB?
I wrote a little test script to compare PHP PDO versus PEAR, and here are the results:
I’m simply doing a query to return a few rows, and discarding them.
First, using PEAR DB, opening and closing connections:
2 consecutive calls on average: 0.0582370758057 seconds
20 consecutive calls on average: 0.292555093765 seconds
200 consecutive calls on average: 2.75805211067 seconds
2000 consecutive calls on average: 25.285836935 seconds
Next, using PHP PDO, opening and closing connections:
2 consecutive calls on average: 0.0467140674591
20 consecutive calls on average: 0.274536132812 seconds
200 consecutive calls on average: 2.57240796089 seconds
2000 consecutive calls on average: 24.2841658592 seconds
I then turned PHP PDO’s persistent connection on:
2 consecutive calls on average: 0.0204339027405 seconds
20 consecutive calls on average: 0.0369820594788 seconds
200 consecutive calls on average: 0.250319957733 seconds
2000 consecutive calls on average: 2.66390395164 seconds
To be fair, I commented out the disconnect() from the PEAR library in between calls:
2 consecutive calls on average: 0.0467071533203 seconds
20 consecutive calls on average: 0.0733239650726 seconds
200 consecutive calls on average: 0.415247917175 seconds
2000 consecutive calls on average: 4.10179281235 seconds
and commented out the $dbh = null (disconnect) from the PDO Persistent calls:
2 consecutive calls on average: 0.0203170776367 seconds
20 consecutive calls on average: 0.0376739501953 seconds
200 consecutive calls on average: 0.259738922119 seconds
2000 consecutive calls on average: 2.4946231842 seconds
But in between page loads, the disconnect is always called. More testing is needed to see if persistence is achieved between page loads, but my guess is no.
try {
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
for ($i=0; $i<1000; $i++) {
$dbh = new PDO( 'pgsql:host=localhost;dbname=new_test',
'postgres',
'nopassword' );
// PHP 5.1 specific
// array( PDO::ATTR_PERSISTENT => true ) );
// PHP 5.0 specific
//array( PDO_ATTR_PERSISTENT => true ) );
// make initial connection
foreach ($dbh->query('SELECT * from users;') as $row) {
// print_r($row);
$bob = $row;
}
// close the connection
$dbh = null;
// make another connection after
$dbh = new PDO( 'pgsql:host=localhost;dbname=new_test',
'postgres',
'nopassword' );
// PHP 5.1 specific
// array( PDO::ATTR_PERSISTENT => true ) );
// PHP 5.0 specific
// array( PDO_ATTR_PERSISTENT => true ) );
foreach ($dbh->query('SELECT * from users;') as $row) {
//print_r($row);
$bob = $row;
}
$dbh = null;
}
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "This page was created in ".$totaltime." seconds";
}
catch (PDOException $e) {
//print_r($e);
// if cannot connect to DB, fatal DB error -
// handle in global exception handler
throw new DatabaseException();
}
