I just finished testing out some of my scripts using ab, the apache benchmark utility and while i was running ab I would run top on the server to check memory usage and cpu utilization. Suffice it to say, i was unable to determine if one particular script was more efficient than the other. The results were varying too much. However, just using ab for execution times did give me something to start thinking about. For example this code, which would you think would be more efficient.
// ############## Which is more efficient ####################
// ############## USING echo and keeping the data in the buffer ####################
ob_start();
for($i=0; $i<1000; $i++) {
echo 'Very long STring';
}
$test = ob_get_contents();
ob_end_clean();
// Misc Code here
echo $test;
// ############## OR just storing this in a variable ####################
$a = null;
for($i=0; $i<1000; $i++) {
$a .= 'Very long STring';
}
// Misc Code here
echo $a;
Using ob_start ended up being faster, yet I thought they would be the same. Don't they both use the same amount of resources, so why is the one using ob_start running faster?
Here is the same benchark run only once.
http://section31.us/scripts/bench.php
Source: http://section31.us/scripts/bench.phps
h, and if you're asking yourself how much faster. on ab, i ran 100 requests, with 10 connections, and it comes out to 15-20% faster. This is really sad considering i've been using the other way for the whole year and a half i've been using php.