Skip to content

Instantly share code, notes, and snippets.

@julesbou
Created September 12, 2011 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julesbou/1212664 to your computer and use it in GitHub Desktop.
Save julesbou/1212664 to your computer and use it in GitHub Desktop.
[benchmark] text file VS mysql VS apc
<?php
// Usage:
//
// $ php benchmar.php
// or
// $ php benchmark.php 1000 // to execute 1000 times
$pdo = new \PDO('sqlite://:memory:');
$pdo->exec("CREATE TABLE IF NOT EXISTS `Users` (
`id` int AUTO_INCREMENT,
`username` varchar(150) NOT NULL,
`score` mediumint(8) NOT NULL DEFAULT '0',
`password` varchar(225) NOT NULL,
`gravatar_url` varchar(155) DEFAULT NULL,
);
");
$pdo->exec('DELETE FROM Users');
$count = isset($argv[1]) ? (int)$argv[1] : 100;
for ($i = 0; $i < $count; $i++) {
$pdo->exec("INSERT INTO `Users` VALUES(".($i+1).", 'coco', 275, 'f25966edfc9b', 'url');");
}
echo "$count rows\n";
// with database
$start = microtime(true);
$rows = $pdo->query('SELECT * FROM `Users`')->fetchAll();
$end = (microtime(true) - $start) * 1000;
echo "with database: $end ms\n";
// store data
file_put_contents('benchmark_data_file.txt', json_encode($rows));
apc_store('benchmark_data', json_encode($rows));
// with text file
$start = microtime(true);
$rows = json_decode(file_get_contents('test.txt'), true);
$end = (microtime(true) - $start) * 1000;
echo "with text file: $end ms\n";
// with apc
$start = microtime(true);
$rows = apc_fetch('test');
$end = (microtime(true) - $start) * 1000;
echo "with apc cache: $end ms\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment