<?php
// Which query to test?
//$sql = "SELECT NULL";
$sql = "SHOW TABLE STATUS";
//$sql = "SELECT * FROM users";
// Your database credentials
$dbname = 'database';
$dbuser = 'username';
$dbpass = 'password';
// How many times to run the tests?
$runs = 10000;
// Wait how many seconds before ending the script?
// Don't render as HTML
header('Content-Type: text/plain');
function testMySQLi() {
global $dbuser,
$dbpass,
$dbname,
$sql;
$mdb = new mysqli('localhost',$dbuser,$dbpass,$dbname);
$result = $mdb->query($sql);
while ($row = $result->fetch_row())
//while ($row = $result->fetch_array()) // fetch_row() is faster but not available in PDO
continue;
}
function testPDO() {
global $dbuser,
$dbpass,
$dbname,
$sql;
$pdb = new PDO('mysql:host=localhost;dbname='.$dbname,$dbuser,$dbpass);
foreach ($pdb->query($sql) as $row)
continue;
}
for ($c=-5;$c<=$runs;$c++) {
$Ms[] = testMySQLi();
$Ps[] = testPDO();
// Ignore the first few runs
if ($c==
0) $Ps =
$Ms =
array();
}
echo "# of runs: $runs\n";
echo "Total time for mysqli(): $Mt\n";
//print_r($Ms);
echo "Total time for PDO(): $Pt\n";
//print_r($Ps);
if ($Mt<
$Pt) echo "mysqli was faster by $diff seconds.\n";
else if ($Pt<
$Mt) echo "PGO was faster by $diff seconds.\n";
else echo "The times were equal!\n";