[nycphp-talk] testing a theory
Rob Marscher
rmarscher at beaffinitive.com
Wed Feb 7 12:38:23 EST 2007
OK... I got interested enough to do a test myself. I ran it a few times
with 100,000 loops of concatenating vs. echoing. The average result was
about this:
Concatenation took 0.13881587982178. Multiple echoes took 0.074604988098145.
Concat mem usage was 1760704. Multiple echo mem usage was 901224.
58 milliseconds faster to do multiple echoes and uses about 839Kb less
memory. I'm not sure how reliable get_memory_usage() is for
benchmarking memory.
By the way, in PHP5, you can just do:
$starttime = microtime(true);
But here's the source for my test that works in PHP4 and PHP5. You can
change the iterations number and replace 'some text' with another string
to try out different scenarios.
Later,
Rob
<?php
// mimic php5's microtime(true)
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$iterations = 100000;
// because I don't really want to see the output
ob_start();
if (function_exists('memory_get_usage')) {
$memstart = memory_get_usage();
}
$start = microtime_float();
for ($i = 0; $i < $iterations; $i++)
{
$content .= 'some text';
}
echo $content;
$end = microtime_float();
if (function_exists('memory_get_usage')) {
$memend = memory_get_usage();
}
$test1time = $end - $start;
if ($memstart && $memend) {
$test1mem = $memend - $memstart;
}
if (function_exists('memory_get_usage')) {
$memstart = memory_get_usage();
}
$start = microtime_float();
for ($i = 0; $i < $iterations; $i++)
{
echo 'some text';
}
$end = microtime_float();
if (function_exists('memory_get_usage')) {
$memend = memory_get_usage();
}
$test2time = $end - $start;
if ($memstart && $memend) {
$test2mem = $memend - $memstart;
}
// discard the output
ob_end_clean();
echo "Concatenation took $test1time. Multiple echoes took $test2time.\n";
if (isset($test1mem) && isset($test2mem)) {
echo "Concat mem usage was $test1mem. Multiple echo mem usage was
$test2mem.\n";
} else {
echo "Unable to determine memory usage. Memory limit is not enabled.\n";
}
?>
More information about the talk
mailing list