Created
June 14, 2012 10:30
-
-
Save mbernson/2929492 to your computer and use it in GitHub Desktop.
PDO versus ext/mysql test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE DATABASE `php_mysqltest`; | |
CREATE TABLE `test` ( | |
`id` int(11) unsigned NOT NULL AUTO_INCREMENT, | |
`test_int` int(11) DEFAULT NULL, | |
`test_float` float DEFAULT NULL, | |
`test_string` varchar(40) DEFAULT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=MyISAM DEFAULT CHARSET=utf8; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$startTime = microtime(true); | |
$dbh = new PDO('mysql:dbname=php_mysqltest;host=localhost', 'php', 'php'); | |
for($i = 0; $i < 90000; $i++) { | |
$st = $dbh->prepare('INSERT INTO test (`test_int`, `test_float`, `test_string`) VALUES (:int, :float, :string)'); | |
$st->bindValue(':int', mt_rand(), PDO::PARAM_INT); | |
$st->bindValue(':float', mt_rand()/mt_rand()*10); | |
$st->bindValue(':string', sha1(mt_rand())); | |
$st->execute(); | |
unset($st); | |
} | |
$endTime = microtime(true); | |
echo 'Inserting ', $i, ' rows took ', $endTime - $startTime, ' seconds'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$startTime = microtime(true); | |
mysql_connect('localhost', 'php', 'php'); | |
mysql_select_db('php_mysqltest'); | |
$q = mysql_query('SELECT * FROM test'); | |
$result = array(); | |
while($row = mysql_fetch_assoc($q)) { | |
$result[] = $row; | |
} | |
header('Content-Type: application/json'); | |
echo json_encode($result); | |
$endTime = microtime(true); | |
echo 'Query took ', $endTime - $startTime, ' seconds'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$startTime = microtime(true); | |
$dbh = new PDO('mysql:dbname=php_mysqltest;host=localhost', 'php', 'php'); | |
$result = $dbh->query('SELECT * FROM test')->fetchAll(PDO::FETCH_ASSOC); | |
header('Content-Type: application/json'); | |
echo json_encode($result); | |
$endTime = microtime(true); | |
echo 'Query took ', $endTime - $startTime, ' seconds'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment