Created
May 7, 2020 06:18
-
-
Save caramelchocolate/34abba6b535cc20271c70516901931bb to your computer and use it in GitHub Desktop.
include vs ob_get_clean benchmark
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 | |
$time = microtime(true); | |
register_shutdown_function( | |
function () use ($time) { | |
$time = microtime(true) - $time; | |
$format = <<<EOD | |
Memory: %s / %s MB | |
Time: %f ms | |
EOD; | |
fwrite(STDERR, | |
sprintf($format, | |
round((memory_get_usage() / pow(1024, 2)), 4), | |
round((memory_get_peak_usage() / pow(1024, 2)), 4), | |
($time * 1000))); | |
} | |
); | |
$a = function() { | |
include 'index.php'; | |
}; | |
$a(); | |
?> |
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 | |
makehtml(10000); | |
echo 'include.php' . PHP_EOL; | |
echo shell_exec('php include.php > /dev/null'); | |
echo PHP_EOL; | |
echo 'ob_get_clean.php' . PHP_EOL; | |
echo shell_exec('php ob_get_clean.php > /dev/null'); | |
function makehtml($max_row) { | |
$file = './index.php'; | |
if (file_exists($file)) { | |
unlink($file); | |
} | |
$data = <<<'EOD' | |
<html> | |
<body> | |
EOD; | |
file_put_contents($file, $data, FILE_APPEND); | |
for ($i=0; $i<$max_row; $i++) { | |
$data = '<p>' . bin2hex(openssl_random_pseudo_bytes(64)) . '</p>' . PHP_EOL; | |
file_put_contents($file, $data, FILE_APPEND); | |
} | |
$data = <<<'EOD' | |
</body> | |
</html> | |
EOD; | |
file_put_contents($file, $data, FILE_APPEND); | |
} | |
?> |
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 | |
$time = microtime(true); | |
register_shutdown_function( | |
function () use ($time) { | |
$time = microtime(true) - $time; | |
$format = <<<EOD | |
Memory: %s / %s MB | |
Time: %f ms | |
EOD; | |
fwrite(STDERR, | |
sprintf($format, | |
round((memory_get_usage() / pow(1024, 2)), 4), | |
round((memory_get_peak_usage() / pow(1024, 2)), 4), | |
($time * 1000))); | |
} | |
); | |
$a = function () { | |
ob_start(); | |
include 'index.php'; | |
$output = ob_get_clean(); | |
echo $output; | |
}; | |
$a(); | |
?> |
Author
caramelchocolate
commented
May 7, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment