Last active
March 21, 2021 08:32
-
-
Save CQD/745793eea05e86925a646184ac6f70db to your computer and use it in GitHub Desktop.
https://www.ptt.cc/bbs/PHP/M.1616315184.A.7B0.html 的 benchmark 範例
This file contains 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
[16:19:03] $ php speed.php 1000 | |
loopSize: 1000 | |
sequence: sqliteWrite sqliteRead plainFileWrite | |
sqliteWrite took 0.226 sec | |
sqliteRead took 0.254 sec | |
plainFileWrite took 0.131 sec | |
################################## | |
[16:20:38] $ php speed.php 1 | |
loopSize: 1 | |
sequence: sqliteWrite sqliteRead plainFileWrite | |
sqliteWrite took 0.005 sec | |
sqliteRead took 0.003 sec | |
plainFileWrite took 0.001 sec |
This file contains 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 | |
$loopSize = $argv[1] ?? 1000; | |
$sequence = array_slice($argv, 2) ?: ['sqliteWrite', 'sqliteRead', 'plainFileWrite']; | |
printf("loopSize: %d\nsequence: %s\n\n", $loopSize, implode(' ', $sequence)); | |
foreach ($sequence as $func) { | |
$startTime = microtime(true); | |
$func($loopSize); | |
printf("%10s took %.3f sec\n", $func, microtime(true) - $startTime); | |
} | |
///////////////////// | |
function sqliteWrite($loopSize) | |
{ | |
$pdo = new PDO('sqlite:/tmp/speed.db'); | |
$pdo->exec('CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT)'); | |
for ($i = 0; $i < $loopSize; $i++) { | |
$pdo = new PDO('sqlite:/tmp/speed.db'); | |
$pdo->exec("INSERT INTO test (name) VALUES ('Value {$i}')"); | |
} | |
unlink('/tmp/speed.db'); | |
} | |
function sqliteRead($loopSize) | |
{ | |
$pdo = new PDO('sqlite:/tmp/speed.db'); | |
$pdo->exec('CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT)'); | |
$pdo->exec("INSERT INTO test (name) VALUES ('Value 1')"); | |
for ($i = 0; $i < $loopSize; $i++) { | |
$pdo = new PDO('sqlite:/tmp/speed.db'); | |
$pdo->exec("SELECT * FROM test LIMIT 1"); | |
} | |
unlink('/tmp/speed.db'); | |
} | |
function plainFileWrite($loopSize) | |
{ | |
touch('/tmp/speed.txt'); | |
for ($i = 0; $i < $loopSize; $i++) { | |
file_put_contents('/tmp/speed.txt', "Value {$i}\n", FILE_APPEND); | |
} | |
unlink('/tmp/speed.txt'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment